Product Type Pricing Rules in Drupal Commerce

Recently, I wanted to do rule-based pricing in Drupal Commerce, but only for a certain product type. Commerce ships with a rule to react on “Calculating the Sell Price of a Product” and an associated set of actions to discount by a percentage (10% off!), fixed amount ($10 off!), or set a fixed price (only $10!).

This is what you’ll typically find documented, but I got tripped up trying to add a condition to only apply a discount to certain product types instead of globally discounting products (all square widgets only $10!). By default, though, I wasn’t able to use the line item’s product type in a data comparison condition.

Turns out you can do this, but, as is so often the case, you need to figure out what magical incantation will give you the rules scope you need. In this case, adding an “Entity has field” condition to check that the entity has the ‘commerce_product’ field does the trick. From there, you’ll be able to add another condition and reference the commerce-line-item:commerce-product: selector to do your comparison.

Update: I’ve posted a sample rule for those who would like to see a complete implementation.

3 thoughts on “Product Type Pricing Rules in Drupal Commerce

  1. Hey man,

    exactly what I was looking for as I need to add 20% to only some of my product types.

    Apart from rules and the data selection is very confusing for me at the minute. I added the entity has field part ok but when adding the next condition, what condition do I add from the drop down list.

    Can’t believe how confusing this is to simply add a tax amount to some product types. Am I missing something?

    • I agree it can be confusing. The way I’ve written this rule up for my project, my next condition in the rule is a data comparison. If you’ve already pulled the commerce-product field into scope, you should have the commerce-line-item:commerce-product:type data selector available in this condition, which will let you check that the product being priced is the type you want to adjust price for.

      I’ve gone ahead and exported an example rule that demonstrates this, it’s available at https://gist.github.com/Pythagory/f46da35a057a85a1268a

Leave a Reply

Your email address will not be published. Required fields are marked *