Applying discounts on certain product types with Drupal Commerce

Drupal discount on product types

Drupal Commerce allows setting up sort of discounts out of the box using Commerce Pricing Rules. This feature is available once you enable Product Pricing UI sub-module, and some of the default pricing rules require having Tax UI enables as well. On the website I inherited, there was a several pricing rules, created manually, that set certain discount percentage to certain user roles, which was similarly to this guide from the Drupal Commerce website. The goal is to prevent applying discounts on products of certain type. In this article, I will show you how to achieve that with Rules.

Firstly, I have to say that I am no expert in Drupal Commerce or Rules module, and even though the task seems really easy, it took some time for me to figure it out. Let’s go through the process of creating a pricing rule step-by-step.

To see all your existing pricing rules, go to Store » Configuration » Product pricing rules on your admin menu. If you do not have this section, make sure that Product Pricing UI is enabled. Otherwise click on the Add the pricing list to get started.

Now let’s configure events that would fire this rule. By default, there’s only one event, Calculating the sell price of a product, but if you want the calculated price to appear technically everywhere instead of the price specified in the product, you’d better add Commerce Product is viewed and Commerce Line item is viewed rules.

Pricing rules add events

The second step is conditions – probably the most important step in the process. Here we should set one or multiple conditions on which the action would fire. For instance, if you want to apply a discount to a certain user role, you will need to add the User has role(s) condition and specify one or more of the existing roles for which it should work. However, as the title of this post says, we want to affect only products of certain type. This seemed to me difficult at first, as there’s no way to compare product’s properties for now. But do not worry. Rules may not let us to access any entity before it is referenced from the previous condition. In our case, before comparing commerce product’s rules, we need to make sure that commerce line item actually has commerce products. To do that, add Entity has field condition and check if it has the Commerce Product field, as shown on the image:

Check if Entity has commerce product

If this condition passes, in the following ones, the commerce product entity will be available (we referenced it, so to speak), and we will be able to check if it belongs (or not belongs) to a certain product type, in the conditions below. Now let’s add a simple data comparison condition.

Rules Data Comparison

Now we can use selector commerce-line-item:commerce-product:type (because if the “code” execution came to this, we certainly know that this commerce line item has commerce product, and each commerce product has a mandatory type field). Further we check if the product type is equal (or is one of) a certain value, and if it is, the action is to be fired, and eventually the discount gets applied. It is also possible to use the “not equals” operator by checking “Negate” checkbox on the bottom.

Discount Action

And the final step – adding an action. There is a bunch of action available under the commerce line item section. The one we’re going to use is “Multiply the unit price by some amount”. This is perfect for percentage discounts: if you want to set 50% discount, you have to multiply the initial price by 0.5; for 75% the multiplier will be 0.75, and so on. In our case, we select commerce_line_item and multiply its base price by 0.7, will results a 30% discount. You can also apply this calculation to another price component, such as discounts or fees, and choose how to round the result prices. You can read more on Drupal Commerce price components here.

Now you can finally push the Save button, and if you set up everything properly, enjoy your selectively applied discounts. Thank you for reading and hope it has been of some help to you.