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

How to Offer a 50% Discount on Every Third Purchase of Product A in WooCommerce?

Implementing structured BOGO (Buy One, Get One) discount of offering a 50% discount is helpful when the store owners want to spike up the sales of a particular product or captivate customers to buy a specific product in bulk. Such startegy will help to boost the sales of a particular product by offering a consistent pattern of discounts on every third purchase of the product.

Solution: Offer a 50% Discount on Every Third Purchase of Product A in WooCommerce

This code will let you to add a 50% discount to product A on purchase of every 3rd item of product A, and the discount amount increases by 50% of the product price for every 3rd item added to the cart.

add_action('woocommerce_cart_calculate_fees', 'ts_custom_discount_based_on_logic', 10, 1);

function ts_custom_discount_based_on_logic($cart)
{
    if (is_admin() && !defined('DOING_AJAX')) return;

    // YOUR SETTINGS:
    $targeted_product_id = 54; // Set HERE your targeted product ID

    // Initializing variables
    $discount = $qty_notice = 0;
    $parent_prices = array();

    // Loop through cart items
    foreach ($cart->get_cart() as $key => $cart_item) {
        if (in_array($targeted_product_id, [$cart_item['product_id'], $cart_item['variation_id']])) {
            $quantity = (int) $cart_item['quantity'];
            $qty_notice += $quantity;

            // Collect the price of the parent product
            $parent_prices[] = floatval($cart_item['data']->get_price());
        }
    }

    // Check if the quantity is a multiple of 3
    if ($qty_notice % 3 == 0) {
        // Calculate the number of items to apply the discount
        $discounted_items = $qty_notice / 3;

        // Apply 50% discount on every 3rd item
        $discount -= $discounted_items * floatval($parent_prices[0]) * 0.5;
    }

    // Applying the discount
    if ($discount != 0) {
        $cart->add_fee('Custom Discount', $discount, true);

        // Displaying a custom notice (optional)
        wc_clear_notices(); // clear other notices on the checkout page.
        if (!is_checkout()) {
            wc_add_notice(__("50% discount applied on every 3rd item."), 'notice');
        }
    }
    // Display a custom notice on the cart page when quantity is not enough
    elseif ($qty_notice > 0 && $qty_notice % 3 != 0) {
        wc_clear_notices(); // clear other notices on the checkout page.
        if (is_cart()) {
            wc_add_notice(__("Add more items to get the 50% discount on every 3rd item."), 'notice');
        }
    }
}

Output

When a customer purchases the specified product as defined in the code, and if the targeted product id is added to the cart with 1 quantity, the below shown notification appears to prompt customers to get a discount for the 3rd item.

How to Offer a 50% Discount on Every Third Purchase of Product A in WooCommerce? - Tyche Softwares

Scenario: When Quantity is 3

When a customer purchases the specified product as defined in the code, the loop iterates through the cart items, and since the quantity is a multiple of 3, a 50% discount is applied for each 3rd item.

Discount Amount=⌊3/3​⌋×50%×Product Price

How to Offer a 50% Discount on Every Third Purchase of Product A in WooCommerce? - Tyche Softwares

Scenario : When Quantity is 6

  • The loop iterates through the cart items, and since the quantity is a multiple of 3, a 50% discount is applied for each 3rd item.
  • Discount Calculation: Discount Amount=2×50%×Product PriceDiscount Amount=2×50%×Product Price (Two 3rd items receive a 50% discount each).
How to Offer a 50% Discount on Every Third Purchase of Product A in WooCommerce? - Tyche Softwares

Scenario : When Quantity is 9

  • Again, the loop iterates through the cart items, and as the quantity is a multiple of 3, a 50% discount is applied for each 3rd item.
  • Discount Calculation: Discount Amount=3×50%×Product PriceDiscount Amount=3×50%×Product Price (Three 3rd items receive a 50% discount each).
How to Offer a 50% Discount on Every Third Purchase of Product A in WooCommerce? - Tyche Softwares

You can also provide discount tiers based on a defined quantity range. Check out here on how to add dynamic bulk WooCommmerce discount tiers based on quantity.

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

Share It:

Subscribe
Notify of
2 Comments
Newest
Oldest
Inline Feedbacks
View all comments
László
4 months ago

Hello,
I would like to know how to set it to be true for any product.
Thanks
Laszlo

2
0
Would love your thoughts, please comment.x
()
x