1. Home
  2. Knowledge Base
  3. WooCommerce Product Options
  4. Price Formulas

Price formula example: Apply a discount based on the number of choices selected

In addition to sales or other regular discounts, stores might need more complex models to calcualate the price of their products. You can do this with the price formula in WooCommerce Product Options. In this article, we will explore two examples of calculating a "package discount" based on the number of that the customer selects for a given option.

The linear model

In this first example, a 10% discount will be applied to the main product price for each accessory added to the cart together with the main product, up to a maximum 50% off.

What we need for this configuration is:

  • a Checkboxes option with the choices for the accessories. We will call this option Accessories;
  • a Price formula option which calculates the final discount (not the final price!) based on the number of choices customers select for Accessories. We will call this option Package discount. and will also keep the Ignore main product price setting unchecked.

The formula that calculates the discount is:

-0.1 * [product_price] * min( [Accessories.count, 5] )

Let's examine this formula. -0.1 * [product_price] represents a negative 10% of the main product price. It is negative because the formula will calculate how much will be subtracted from the final total.  The next factor, min( [Accessories.count], 5 ) returns the number of accessories being selected, capped at 5. The use of the min() function to calculate a maximum value might be counterintuitive but it is easy to dispel any doubts with the following observation. When [Accessories.count] is 0, the minimum between 0 and 5 is 0, which is the expected result because no choice was selected so no discount will be applied to the product. When [Accessories.count] is 1, the minimum between 1 and 5 is 1, which again is the expected result. This observation can be extended to every number up to 5 with the same result. But what happens when [Accessories.count] is 6? In that case min( 6, 5 ) returns 5, which means that the number of choices selected has been capped at 5. That explains why we use the min() function to max out a variable to a certain limit: the result of that function must be the minimum between our variable and its limit.

If the product costs $599.00, for every choice being selected a $59.90 discount will be applied to the final price, up to a maximum of $299.50 discount (which is 50% of our main product price). Let's see how the formula above is calculated when [Accessories.count] is 4.

-0.1 * 599 * min( 4, 5 ) = -0.1 * 599 * 4 = -239.6

So, in the end, the discount applied to the final total is -$239.60. The actual total will depend on the fee of each choice but customers will see the appropriate discount applied to it.

The non-linear model

In the previous example, an additional 10% discount on the main product price was applied to the total for every choice selected. But what if the store owner wants to apply a different discount depending on the number of choices selected? Let's say that they want to give a 3% discount when 1 choice is selected, 8% when 2 choices are selected, 15% for 3 choices, and 25% for 4 choices or more. As you can see, the discount is not proportional to the number of choices selected. If it were, with a 3% discount for one choice, the discount would be 6% for 2, 9% for 3 and 12% for 4 or more. But in this case, we start with 3% and we add an increasingly larger percentage at every subsequent choice (5%, 7%, and 10%, respectively).

As in the previous example, what we need for this configuration is:

  • a Checkboxes option with the choices for the accessories. We will call this option Accessories;
  • a Price formula option which calculates the final discount (not the final price!) based on the number of choices customers select for Accessories. We will call this option Package discount. and will also keep the Ignore main product price setting unchecked.

To get the required result, we will take advantage of the bulkRate() function and the formula calculating the total discount will look as follows:

-1 * bulkRate( [product_price], [Accessories.count], 1, 0.03, 2, 0.08, 3, 0.15, 4, 0.25 )

As in the previous example, -1 makes the whole result of the price formula a negative number (because this is going to be shown as a discount). The bulkRate() function returns the amount being discounted. The arguments passed to that function are, in sequence, the product price, the number of choices selected, and as many duplets as the tiers we have in this price model. The duplets indicate, pair by pair, the bottom limit of the tier and the number used for that tier. So, here we are instructing the bulkRate() function to use 3% (0.03) when the choice selected is 1, 8% (0.08) when they are 2, 15% (0.15) when they are 3, and 25% (0.25) when they are 4 or more.

Related Articles

If searching the knowledge base hasn't answered your question, please contact support.