Product Pricing Rules (with screencasts)

Discounts in Drupal Commerce

Overview

  • Create a discount using a rule
  • Create a weekend (date-based) sale discount using a rule
  • Create a tag-based weekend sale discount using a rule

Simple Discount

An example of a simple discount: You want to take 10% off everything in your store until further notice.

  1. Go to Store -> Configuration -> Product Pricing Rules
  2. Click "Add a pricing rule"
  3. Give your rule a name
  4. Under "Actions" click "Add Action"
  5. Under "Commmerce Line Item" choose "Multiply the Unit Price by Some Amount"
  6. The data selector should be "line_item"
  7. The amount should be .9 (in other words, multiply the price by .9).
  8. Now all products have 10% taken off of them.

Adding a Date Window to the Discount

Now we need to take our simple discount and make it effective for just one period of time. We'll use the "Simple Discount" approach above, but now we'll make it only applicable during this upcoming weekend.

  1. Create a discount as in the "Simple Discount" above.
  2. This time we'll add two conditions to the product pricing rule:
    • System Date is Greater than Midnight April 23
    • System Date is Less than Midnight April 25.

That's it. To test in a Linux environment you can temporarily change the server date using

sudo date MMDDHHMM

for example

sudo date 04231237

Don't forget to change the time back. Changing it like this makes a big mess of things.
sudo ntpdate pool.ntp.org

Drupal Commerce Date-Based Discounts from Randy Fay on Vimeo.

Adding a Taxonomy Term To Drive Discounts

Now we're going to go much farther. We'll add a taxonomy term to our product entity to determine which sale events it should be subject to, and also add a date range.

There is a rather major complexity in this one. Rules does not have the ability currently to follow references (like product references and term references) very well, so we have to jump through some hoops to get access to them. It is do-able, but a bit mind-bending. We hope to see the rules issue relating to this land soon, but for now we have to use the tools we have.

  1. Add a taxonomy vocabulary called "Sales Events"
  2. Add a term to the vocabulary called "April Weekend Madness Sale"
  3. Add a term reference field to the "Product" product type referencing "Sales Events"
  4. Set the "Sales Events" value in one or more products to "April Weekend Madness Sale". We're essentially marking them for this sale.
  5. Create a rules component that determines whether the weekend is here:
    • Administer -> Configuration -> Workflow -> Rules -> Components -> Add new component.
    • Component plugin is "Condition set (AND)"
    • Name is "Weekend of April 23"
    • Continue
    • Add two conditions which do data comparison against system date to select the weekend timeframe just as we did in the earlier, simpler example.
  6. Create another rules component that accepts a product as a variable which will determine whether the product is marked to be part of the sale. Note that the first three conditions we're adding here are just to force Rules to understand how to traverse the data. They seem quite complex at first, but it's actually just cookbook stuff.
    • Administer -> Configuration -> Workflow -> Rules -> Components -> Add new component.
    • Condition set (AND)
    • Name: "Product is on sale"
    • Variables: Data type "Entity - Product", Label "Product", Machine name "product"
    • Add a condition
    • Entity has field
    • Data selector: product
    • Field: field_sales_event
    • Add another condition
    • Data value is empty
    • Data selector: product:field-sales-event
    • Click negate.
    • Add another condition
    • Data value is empty
    • Data selector: product:field-sales-event:tid
    • Click negate.
    • Add a final condition
    • Data comparison
    • Data Selector: product:field-sales-event:tid
    • Value equals (the tid of the taxonomy term we created)
  7. Now we'll create a product pricing rule at Store -> Configuration -> Product Pricing Rules.

    • Add a pricing rule with the name "April weekend sale"
    • Under actions, add an action to multiply the price by 0.8 (we'll give them an even bigger discount than we did last time!)
    • Under conditions:
    • Add condition "Component: April Weekend Sale", which will add the date-based conditions to our rule.
    • Add condition "Entity has field" (This is just to bring the product into scope due to the Rules issue mentioned above.)
      • Data selector: line-item
      • Field: commerce_product
    • Add condition "Component: Product is on sale."
      • Data selector: line-item:commerce-product

    Now we have a product pricing rule that does two things. First it checks the conditions in the "Weekend of April 23" to see if the date is correct, and then it uses the "Product is on sale" component (to which we pass the line item's product) to determine whether the item is on this sale.

    We can change the server date to experiment, and should see that the products we marked now have their sale prices.

Drupal Commerce Complex Pricing Rules from Randy Fay on Vimeo.

Found errors? Think you can improve this documentation? edit this page