WooCommerce: Bulk Dynamic Pricing Without a Plugin

You’re looking to assign different unit prices based on the quantity added to Cart, for example from 1-100 price is $5, from 101-1000 price is $4.90 and from 1001 units ordered price becomes $4.75.

There are many “Dynamic Pricing” plugins out there (and the number #2 on this article would suit complex pricing rules and dynamic discounts), but this time I want to teach you a simple code to DIY WooCommerce quantity-based pricing.

As usual, comments and shares are much appreciated. Enjoy!

Screenshot of a WooCommerce product with โ‚ฌ34 price per unit below our bulk pricing thresholds (more screenshots below)

PHP Snippet: Change Product Price Based on Quantity Added to Cart (Bulk Pricing)

In our example, our product price is โ‚ฌ34 and I want to apply a 5% discount above 100 units and a 10% discount above 1000 units. Screenshots for threshold 1 and threshold 2 are below the snippet.

/**
 * @snippet       Bulk (Dynamic) Pricing - WooCommerce
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.6.4
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_action( 'woocommerce_before_calculate_totals', 'bbloomer_quantity_based_pricing', 9999 );

function bbloomer_quantity_based_pricing( $cart ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;

    // Define discount rules and thresholds
    $threshold1 = 101; // Change price if items > 100
    $discount1 = 0.05; // Reduce unit price by 5%
    $threshold2 = 1001; // Change price if items > 1000
    $discount2 = 0.1; // Reduce unit price by 10%

    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
		$product_id = $cart_item['product_id'];
		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 );
		} 	
    }
	
 }
Threshold 1 reached: 5% discount
Threshold 2 reached: 10% discount

Where to add this code?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such delicate files - if you need more guidance, please take a look at my free WooCommerce Customization video tutorial.

Does this snippet still work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free videos, tutorials and tips!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP
  • woocommerce-customize-shop-page-PHP
  • woocommerce-advanced-customization
  • how-to-edit-woocommerce-cart
  • woocommerce-customize-checkout-page-PHP
  • woocommerce-email-customization
  • woocommerce-conditional-logic

Rodolfo Melogli

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as a WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

6 thoughts on “WooCommerce: Bulk Dynamic Pricing Without a Plugin

  1. Hi
    Great … Can the settings ( my price rule) show up on the single product page? So the customer can see if he/she added a few more the price would drop?

    I am only targeting 1-2 products that both have variants and quantity is low:
    1 = full price (100$)
    3 = 10% off / OR 89$ pr. item
    5 = 15% off / OR 85$ pr. item

    Best regards

    Peter

    1. Hello Peter, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  2. I’m wondering if this could be amended to shipping costs

    – For example if a “flat” rate were set to say: $10 per order. Then if more than one item were placed in the cart, the ( $cart_item[‘quantity’] ) would check for more than one – and use the threshold function to apply it to the “flat rate” amount replacing it with: $14 instead.

    What do you think?

  3. Congratulations ! I think it’s a good post to do this action in an online Woocommerce store without using a plugin. Is it possible to apply different price and different quantity conditions for each product? Thank you

    1. Thanks so much! Yes, it is possible – I suggest you take a look at “conditional logic”: https://businessbloomer.com/conditional-logic-woocommerce-tutorial/ and https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/. Let me know ๐Ÿ™‚

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between: [php]code_here[/php]. Failure to complying with this (as well as going off topic) will result in comment deletion. You should expect a reply in about a week - this is a popular blog but I need to get paid work done first. Please consider joining #BloomerArmada to ask me 1-to-1 WooCommerce questions. Thank you :)

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.