This WooCommerce cart discount strategy may seem simple, offering a 20% discount based on the cart subtotal threshold, but it’s incredibly effective. The best part is that it provides real-time notifications to inform customers how close they are to receiving a discount, making their shopping experience more engaging. With this strategy, you can easily persuade customers to add more items to their cart to reach the discount threshold and boost your sales.
Solution: Offer a 20% Discount on the Next Quantity of Product A Based on WooCommerce Cart Total
The code implements a dynamic discount based on the quantity of a specific product (Product A) in the WooCommerce cart and the total cart subtotal.
// Hook to apply discount based on cart subtotal add_action('woocommerce_before_calculate_totals', 'ts_apply_discount_based_on_subtotal'); function ts_apply_discount_based_on_subtotal($cart) { if (is_admin() && !defined('DOING_AJAX')) { return; } // YOUR SETTINGS: $targeted_product_id = 730; // Set HERE your targeted product ID $discount_percentage = 20; $subtotal_threshold = 100; $product_a_quantity = 0; $subtotal = 0; $discount_applied = false; // Loop through cart items to calculate quantities and subtotal foreach ($cart->get_cart() as $cart_item) { if ($cart_item['product_id'] == $targeted_product_id) { $product_a_quantity += $cart_item['quantity']; } $subtotal += $cart_item['line_total'] + $cart_item['line_tax']; } // Check if product A is in the cart, subtotal is close to the threshold, and discount has not been applied if ($product_a_quantity > 0 && $subtotal > ($subtotal_threshold - $discount_percentage) && !$discount_applied) { // Display notice on the cart and checkout pages wc_clear_notices(); // clear other notices on the checkout page. if (is_cart() || is_checkout()) { wc_add_notice(sprintf(__('Add the next quantity & get 20%% discount. Only %s away from the threshold!'), wc_price($subtotal_threshold - $subtotal)), 'notice'); } } // Check if product A is in the cart, subtotal exceeds the threshold, and discount has not been applied if ($product_a_quantity > 0 && $subtotal > $subtotal_threshold && !$discount_applied) { // Apply the discount to the next quantity of product A foreach ($cart->get_cart() as $cart_item) { if ($cart_item['product_id'] == $targeted_product_id) { // Calculate the next quantity of product A $next_quantity = $cart_item['quantity'] + 1; // Calculate the discount amount for the next quantity of product A $discount_amount = $cart_item['data']->get_price() * ($discount_percentage / 100); // Apply the discount by reducing the price for the next quantity $new_price = max(0, $cart_item['data']->get_price() - $discount_amount); $cart_item['data']->set_price($new_price); // Set the flag to indicate that the discount has been applied $discount_applied = true; // Add a discount fee to the cart (optional, based on your requirement) $cart->add_fee(__('Discount for Product A', 'your-text-domain'), -$discount_amount, true); // Display notice on the cart and checkout pages wc_clear_notices(); // clear other notices on the checkout page. if (is_cart() || is_checkout()) { wc_add_notice(__("Congrats! You have got a 20% discount"), 'notice'); } // Break out of the loop once the discount is applied break; } } } }
Output
If a customer adds Product A to the cart, checks if the subtotal (including all line items) exceeds the subtotal threshold of $100, and if it does, apply a 20% discount to Product when the next quantity of product A is added to the cart. Also, display a notice on the cart page, indicating how close the customer is to getting a 20% discount on Product A.
![How to Offer a 20% Discount on the Next Quantity of Product A Based on WooCommerce Cart Total? 1 How to Offer a 20% Discount on the Next Quantity of Product A Based on WooCommerce Cart Total? - Tyche Softwares](https://media.tychesoftwares.com/wp-content/uploads/discount-for-next-quantity-1024x622.png)
When the subtotal threshold is met, the discount of 20%is applied to the price of Product A as shown below.
![How to Offer a 20% Discount on the Next Quantity of Product A Based on WooCommerce Cart Total? 2 How to Offer a 20% Discount on the Next Quantity of Product A Based on WooCommerce Cart Total? - Tyche Softwares](https://media.tychesoftwares.com/wp-content/uploads/Discount-applied-to-the-next-quantity-of-Product-A-1024x940.png)
Alternatively, if you are looking to add a rewarding twist to customers shopping experience then consider adding buy one get one offers based on WooCommerce cart total ranges in WooCommerce.