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        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.8
 * @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 ) {
		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 snippet?

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 files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"

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 Customization?

Check out these free video tutorials. You can start learning how to customize WooCommerce without unnecessary plugins. Watch me code and learn by example!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP

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.

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

  1. Hi!

    Could you give me a help for a specific product only?
    I would like to add a discount only

    product_id == 540

    Thx

    1. Hi there, 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. Hi , I am trying to understand what one line in this code does

     $product_id = $cart_item['product_id'];

    You appear to creating a variable $product_id but I cannot see this variable used anywhere else in the code.
    Can you explain please.

    thank you

    1. You’re right, it’s not needed!

  3. Hi Rodolfo,

    Very informative post for the price object. I was wondering why are you subtracting 1 from the $discount variables? Thanks in advance.

    1. Hi Edder, thank you! It was easier for me. To calculate discounted price, I do price*0.95 in case it’s 5% off (0.05 discount). So the formula actually becomes price*(1-0.05)

  4. Ho Rodolfo,
    I’ve got my shop set up using WooCommerce Product Table – would your code need changing to work? I’ve adjusted to the below to test and price doesn’t change once threshold reached…

    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 = 5; // Change price if items > 4
        $discount1 = 0.05; // Reduce unit price by 5%
        $threshold2 = 10; // Change price if items > 9
        $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'] 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 );
          }   
        }
    
     }
    
    1. Just need to clarify. It does work if I hit “Update Cart” on the cart page. Just wanting price to change on the fly on the shop page if someone reaches the threshold… Ajax I suppose?. Is this possible?

      1. It should work. I see PHP errors in your custom functions, maybe fixing those might help

  5. It did not work for me. Maybe should I try a different setting for my store or something like that? Thank you!

    1. Weird, it should work if you have set up everything correctly. Can you try with a different theme and no other plugins please?

  6. I am looking for a plugin where the customer selects from a drop down menu 1-100 and based on which amount they choose, the price changes. For example default is 1 in the dropdown list and price $100, but if you select 10 in the dropdown list, the price changes to $1000 – can your plugin or code help?

    1. Hi Jake, 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. You can do this without a plugin or integration my friend. Just switch from “Simple Product” to “Variable Product” on the product page and then create your attributes.

  7. Does it work in a multi-currency environment?

    Your code is perfect when running in the default currency. (REALLY APPRECIATE!!), but in multi-currency environment, sounds have some problem,

    To be exact, I found that the displayed price in Single Product Price has double multiplied the currency rate.

    For example: If the product is US$63.95, it should be HK$498 (fix exchange rate between HKD & USD set as 7.8). However, the display price become $63.95 x 7.8 x7.8 = HK$3,890.72…….

    Here’s a video capture for your reference: https://jmp.sh/AxbNJ43
    P.S. I’m Using Villatheme’s MultiCurrency Plugin. (https://villatheme.com/extensions/woo-multi-currency/)
    P.P.S The Above example is: offer 15% discount for order 3 unit or above.

    Great Thanks!

    1. Hello Joe, 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!

  8. Hi,
    Thanks for the code it is amazing! 🙂 Is it possibly to apply it for just specific products ?

    1. Hello Jozsef, 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!

  9. Amazing like always!!!

    Just a question, what do I need to write if I want to add another level (i.e: +5 = 5%, +10=10% and +20=20%) ?

    Thank you for all Rodolpho

    1. Hi Stan, 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!

  10. Hello! Your blog is really great!
    Is there a way to apply a percentage discount to all products older than 100 days from today’s date?
    Without a plugin…

    1. Hey Fabio, 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!

  11. 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!

  12. 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?

  13. 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 *