WooCommerce: How to apply quantity based discounts with and without plugins

One of the most used strategies in commerce, not only online, are volume discounts, a fantastic way to get more sales on each product, but you already knew this, didn’t you?

Of course, with WooCommerce we can also apply this strategy to our online store, and not only with plugins, but with a simple code.

Volume discounts with code

The thing is simple, just add a function like this at the end of the functions.php file of the active child theme or in your customizations plugin:

//Volume discounts
add_action( 'woocommerce_before_calculate_totals', 'wphelp_price_per_quantity', 9999 );
function wphelp_price_per_quantity( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
// Define here thresholds and dicounts
$threshold1 = 11; // Change price if there is > 10 products
$discount1 = 0.05; // 5% discount if there is > 10 products
$threshold2 = 21; // Change price if there is > 20 products
$discount2 = 0.1; // 10% discount if there is > 20 products
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
if ( $cart_item['quantity'] >= $threshold1 && $cart_item['quantity'] < $threshold2 ) {
$price = round( $cart_item['data']->get_price() * ( 1 - $discount1 ), 2 );
$cart_item['data']->set_price( $price );
} elseif ( $cart_item['quantity'] >= $threshold2 ) {
$price = round( $cart_item['data']->get_price() * ( 1 - $discount2 ), 2 );
$cart_item['data']->set_price( $price );
} 
}
}Code language: PHP (php)

In the code above, for each price threshold exceeded ($thresholdx = X) the discount specified in the discount line will be applied ($discountx = X.X)

And you can add as many thresholds and associated discounts as you want, simply by adding their corresponding lines and then their corresponding if and elseif.

Once you have edited the function file to your liking, you can check its operation.

And so on, according to the thresholds and discounts we have set up.

Wouldn’t be better with a plugin?

Sure, you can do that too.

In that case I recommend you to use WooCommerce Dynamic Pricing.

This plugin not only allows you to apply discount rules – or whatever – by volume, but also by category, user profile, total in cart, by product and much more.

These are just a few examples…

Read this post in Spanish: WooCommerce: Cómo aplicar descuentos por volumen con y sin plugins

How useful was this post?

Click on a smiley to rate it!

Average rating 5 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

About The Author

Leave a Comment

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

Scroll to Top
Skip to content