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

How to Set WooCommerce Buy One Get 50% off Discount Only For Specific Number of Products in the Cart?

With this WooCommerce customization, you can focus on clearing out specific products by offering a ‘Buy One, Get 50% Off’ discount. The discount activates when customers add at least two specific items (defined in the code) to their cart, encouraging additional purchases. This approach not only boosts sales but also helps manage stock more effectively.

Solution: Buy One Get 50% off Discount Only For Specific Number of Products in the Cart

This code implements a “Buy One, Get One 50% Off” promotion for specific products in the WooCommerce cart, limiting the discount to a predefined quantity per product.

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

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

    $target_products = array(100, 456, 470); // Replace with your product IDs
    $product_limit = 2; // Set your product limit here
    $discount = 0;

    foreach ($cart->get_cart() as $cart_item) {
        if (in_array($cart_item['product_id'], $target_products)) {
            $qty = min($cart_item['quantity'], $product_limit);
            $product_limit -= $qty;
            $discount -= $qty * ($cart_item['data']->get_price() * 0.5);
            if ($product_limit <= 0) {
                break;
            }
        }
    }

    if ($discount < 0) {
        $cart->add_fee(__('BOGO 50% Off Discount'), $discount);
    }
}
flexi bogo cta banner image


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

If you add ‘N’ items to your cart, you can make sure that ‘Buy One Get One’ discounts only apply to certain products, and you can set a limit on how many items get the discount. In the example below, the discount is applied to only two items, as set in the code.

Set WooCommerce BOGO discount  for specific number of products in the cart

Consider using similar BOGO strategies to boost your average order value. Additionally, let’s explore how to add free gift based on cart quantity in WooCommerce.

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

Share It:

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