Price formula example: Apply a discount based on the number of choices selected
A store sells mobile devices and displays a list of accessories that customers can purchase together with the main product. If a customer purchases a certain number of accessories, then they will get a discount.
This article provides two examples of price formulas that will achieve this result using the price formula field in WooCommerce Product Options.
Note: If you want to give quantity-based discounts based on the quantity of items being purchased then either see this article instead, or use our WooCommerce Quantity Manager plugin for more features.
10% discount if all accessories are selected
What we need in this example is:
- a Checkbox option, with as many choices as the accessories we want to upsell. We will call this option Accessories;
- a Price formula option to calculate the discount.
In this first scenario, the store offers 10% off the price of the main product to customers who decide to purchase all the accessories listed on the page. In this case, the formula is:
[product_price] * ( 1 - 0.1 * [Accessories.all] )
By definition, the all
property of a multiple choice option like checkboxes is 1
only when all the choices of that option are selected and 0
otherwise. So, when [Accessories.all]
is 0
the formula is evaluated as follows:
[product_price] * ( 1 - 0.1 * 0 ) = [product_price] * ( 1 - 0 ) = [product_price]
On the other hand, when all accessories are selected, [Accessories.all]
returns 1
and the formula is evaluated as follows:
[product_price] * ( 1 - 0.1 * 1 ) = [product_price] * ( 1 - 0.1 ) = [product_price] * 0.9
which is 90% of the product price or, in other words, 10% off the product price.
View our demo page to see this formula in action.
If customers purchase at least 3 accessories, the cheapest one is free
What we need in this example is:
- a Products option, displayed as Image buttons, with as many choices as the accessories we want to upsell. We will call this option Accessories;
- a Price formula option to calculate the discount.
In this second scenario, the formula calculates the discount cutting off the price of the cheapest accessory if at least 3 of them are selected. Let's see what the formula looks like:
[product_price] - if( gte( [Accessories.count], 3 ), [Accessories.min], 0 )
The gte()
function returns 1
if the first argument is greater than or equal to the second argument. So, if the number of selected accessories [Accessories.count]
is 3 or more, the condition of the if()
function is true
and the functions returns [Accessories.min]
, which is the minimum price of the selected accessories. Otherwise, the if()
function returns 0
.
View our demo page to see this formula in action.