The feature in this post allows you to create Buy One Get One (BOGO) or similar discount scenarios based on the quantity of items in the cart. The provided code is useful for implementing dynamic bulk pricing for individual product prices based on the selected product quantity in WooCommerce.
This is how the code provided below works: For example, if the quantity of a particular product is greater than or equal to 51 but less than 101, a 5% discount is applied as specified in the code($discount1). If the quantity is 101 or more, a higher 10% discount is applied ($discount2).
add_action( 'woocommerce_before_calculate_totals', 'ts_quantity_based_pricing', 10 ); function ts_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 = 51; // Change price if items > 50 $discount1 = 0.05; // Reduce unit price by 5% $threshold2 = 101; // Change price if items > 100 $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 ); } } }
Output
Let’s look into an example product of the ‘kids chair’ as shown in the image given below. The original price of that product is $10. And, as the quantity of this specific product in the cart does not meet the defined conditions (e.g., not reaching the thresholds of 51 or 101), the discounts won’t be applied.
If the quantity of a product reaches or exceeds the threshold of 51, then the 5% discount is applied to that particular product by adjusting the individual product price of the product.
The discount of 5% remains the same until the quantity of 100. Once the threshold of 101 is attained, as specified in the code, a 10% discount is then applied.
Similar to the above discount process applied, you can also add dynamic bulk WooCommerce discount tiers based on the quantity that will calculate the discount based on the total count of cart items.