2015-11-17
A client of mine wanted a shopping cart that serves her regular and wholesale customers. The trick is her wholesale prices are different per product, so need to be manually entered. Here's how I configured that in Drupal Commerce
Create Wholesale User role
- Create a user role wholesale
Create your Product
- Go to /admin/commerce/products/types or Store » Products » Product Types and create a type of product you need. in my case it is Essential Oil
- For this product type click on manage fields
- Add a new field of the type Price. I called my Wholesale Price with machine name field_wholesale_price
- Configure the field as needed for your currency
- Go to Manage Display for the same product type and make sure that Wholesale Price appears before or after Price field, don't hide either one of these fields
Create product display
- If you haven't done so go to Content Types and create a content type that will hold the product display. Mine was Essential Oil Display
- Under Manage Fields add a Product Reference field and select the appropriate product type; in my case it is Essential Oil
Pricing Rules
Now let's set some product pricing rules for the cart.
- Go to /admin/commerce/config/product-pricing or Store » Configuration » Product Pricing Rules
- Add a new pricing rule and call it Wholesale
- Under Conditions add a new condition User has role and select wholesale; Save
This says the the rule will only apply to users with the wholesalerole. - Add another condtion Data value is empty and select commerce-line-item:line-item-id; check off Negate and Save
We are checking that a line item in our cart is not emtpy; evaluation will stop here if cart is empty. - Add condition Entity has field the entity is commerce-line-item and field value is commerce_product
We are checking that the entity in the cart is a commerce_product to assertain that we are working with expected data. - Add another Data value is empty condition and select commerce-line-item:commerce-product as a selector; checkoff Negate and Save.
This checks that commerce_product is not an empty object. Note if we don't do these checks the fields for the commerce_product will not appear in subsequent conditions. This is the enforcement of graceful failure rules by the Rules module. - Add condition Entity has field and select commerce-line-item:commerce-product as an entity and set field as field_wholesale_price; Save
This checks that we have a wholesale field - Set the last conditon Data value is empty and set commerce-line-item:commerce-product:field-wholesale-price as data to check; check off Negate and Save
This checks that our wholesale price field has a value, else the ruleset fails to apply. - Add new Action and select Set the unit price to a specific amount. Data selector is commerce_line_item; set amount to data selector and set commerce-line-item:commerce-product:field-wholesale-price:amount; leave Price Component Type as Base price and Price Rounding as you see fit; Save
Configuring proper price display per role
I took the path of least resistance and hid the non-applicable price via CSS.
- Configure the product settings to display both prices.
- Update your theme to add a class to the <body> based on user's role (snippet for it below). This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
<?php function mytheme_responsive_preprocess_html(&$variables, $hook) { $body_classes = array($variables['classes_array']); // The body tag's classes are controlled by the $classes_array variable. To // remove a class from $classes_array, use array_diff(). //$variables['classes_array'] = array_diff($variables['classes_array'], array('class-to-remove')); if ($variables['user']) { foreach ($variables['user']->roles as $key => $role) { $role_class = 'role-' . str_replace(' ', '-', $role); array_push($variables['classes_array'], $role_class); } } } - Then in CSS I do something as follows
.field-name-field-wholesale-price { display: none; } .role-wholesale .field-name-field-wholesale-price { display: block; } .field-name-commerce-price { display: block; } .role-wholesale .field-name-commerce-price { display: none; }