Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Add Dynamic Bulk WooCommerce Discount tiers Based on Quantity?

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 code 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);
}

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.
How to Add Dynamic Bulk WooCommerce Discount tiers Based on Quantity? - Tyche Softwares

Similarly, the code applies the corresponding discount of 25% if the quantity of cart items falls within the tiers of 26 to 30.

How to Add Dynamic Bulk WooCommerce Discount tiers Based on Quantity? - Tyche Softwares

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.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x