An easy way to provide tiered discounts or BOGO-like discounts based on the quantity of specific products in the cart is by using plugins in your WooCommerce store. But you can also implement such tiered discounts easily with these WooCommerce snippets.
This code calculates and applies these discounts providing a dynamic and tiered pricing structure based on the total quantity of the items present in the cart. The discount is applied dynamically based on the defined tiers that are configured as follows:
- Tier 1 (1-4 Product): 0% off
- Tier 2 (5-10 Products): 5% off
- Tier 3 (11-15 Products): 10% off
- Tier 4 (16-20 Products): 15% off
- Tier 5 (21-25 Products): 20% off
- Tier 6 (26-30 Products): 25% off
add_action('woocommerce_cart_calculate_fees', 'ts_cart_quantity_discount', 10, 1); function ts_cart_quantity_discount($cart_object) { if (is_admin() && !defined('DOING_AJAX')) { return; } // Define variables $discount = 0; $cart_item_count = $cart_object->get_cart_contents_count(); $cart_total_excl_tax = $cart_object->subtotal_ex_tax; // Define conditional percentage based on the number of items in the cart if ($cart_item_count <= 4) $percent = 0; elseif ($cart_item_count >= 5 && $cart_item_count <= 10) $percent = 5; elseif ($cart_item_count > 10 && $cart_item_count <= 15) $percent = 10; elseif ($cart_item_count > 15 && $cart_item_count <= 20) $percent = 15; elseif ($cart_item_count > 20 && $cart_item_count <= 25) $percent = 20; elseif ($cart_item_count > 25 && $cart_item_count <= 30) $percent = 25; else $percent = 0; // No discount beyond 30 items // Calculate the discount based on the percentage $discount -= ($cart_total_excl_tax / 100) * $percent; // Apply the calculated discount to the cart if ($percent > 0) $cart_object->add_fee(__("Quantity discount $percent%", "woocommerce"), $discount, true); }
This to the shop owners who are running or planning to run BOGO offers on their WooCommerce store…
BOGO deals are great for increasing your sales, but have you thought about which offers are bringing you more revenue and which offers are not performing that great?
Don’t just set a BOGO deal, track the revenue generated by your deals in real-time with the Flexi BOGO for WooCommerce plugin.
Output
When a customer adds the corresponding products specified in the code, these items become eligible for tiered discounts based on their quantity.
In the provided code, the discount is based on the total quantity of the items present in the cart. For example:
- If the cart has a total of 1-4 items, the discount percentage will be 0% in this case.
- If the cart has a total of 5-10 items, the discount percentage will be 5%, and the code will apply a fee of 5% of the subtotal (excluding tax) as a quantity discount.
Similarly, the code applies the corresponding discount of 25% if the quantity of cart items falls within the tiers of 26 to 30.
Similarly, you can also add free gift based on selected quantity in WooCommerce which also alters the free product based on the selected quantity of the cart items.