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

How to Offer a WooCommerce BOGO Deal Based on the Quantity Range of Specific Product?

In one of our post, we have already discussed how to add BOGO Buy One Get One product for a fixed quantity in WooCommerce. While this approach works great for high-priced items, low-cost, daily-use products often benefit more from bulk promotions. For example, setting up a BOGO deal based on a quantity range can be perfect for items that customers regularly buy in bulk. Let’s see how this flexible BOGO deal allows you to encourage higher volume purchases and boost your sales.

Solution: Offer a WooCommerce BOGO Deal Based on the Minimum and Maximum Quantity Range of Specific Product

This code allows customers to receive a free product when they purchase between 5 and 10 units of a specified product (Product A) in WooCommerce.

add_action('template_redirect', 'ts_add_product_to_cart');

function ts_add_product_to_cart() {
    if (!is_admin()) {
        global $woocommerce;

        $product_a_id = 922;
        $free_product_id = 929;

        // Set the targeted quantity range for the parent product A
        $min_targeted_qty = 5;
        $max_targeted_qty = 10;

        // Loop through cart items
        foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
            // Check if the current product is the parent product
            if ($cart_item['product_id'] == $product_a_id) {
                // Check if the quantity of the parent product A falls within the specified range
                if ($cart_item['quantity'] >= $min_targeted_qty && $cart_item['quantity'] <= $max_targeted_qty) {
                    // Check if the free product is already in the cart
                    $free_product_cart_id = WC()->cart->generate_cart_id($free_product_id);
                    $free_product_in_cart = WC()->cart->find_product_in_cart($free_product_cart_id);

                    // If the free product is not in the cart, add it
                    if (!$free_product_in_cart) {
                        WC()->cart->add_to_cart($free_product_id);
                    }
                } else {
                    // If the quantity condition is not met, remove the free product
                    ts_remove_free_product($free_product_id);
                }
            }
        }
    }
}

function ts_remove_free_product($free_product_id) {
    global $woocommerce;

    // Check if the free product is in the cart
    foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) {
        $_product = $values['data'];
        if ($_product->get_id() == $free_product_id) {
            // Remove the free product
            $woocommerce->cart->remove_cart_item($cart_item_key);
        }
    }
}

Output

When a customer adds Coffee Mugs(Parent Product A) to their cart and purchases in bulk quantities the code checks for the below targeted quantity range.

  • minimum targeted quantity=5;
  • maximum targeted quantity =10;

When product A falls within the specified quantity range of 5 to 10 items, a free gift, such as a Coffee Stir Spoon, is automatically added to their cart.

How to Offer a WooCommerce BOGO Deal Based on the Quantity Range of Specific Product? - Tyche Softwares

When the purchased quantity of Coffee Mugs falls below the defined quantity range (5 to 10 items), and as the quantity range threshold is not met, the free gift offer is automatically removed from the customer’s cart.

How to Offer a WooCommerce BOGO Deal Based on the Quantity Range of Specific Product? - Tyche Softwares

So, as you consider the various ways to enhance your WooCommerce store, you could offer similar BOGO Buy One Get One offers based on WooCommerce cart total range instead of product quantity. For instance, you could set up a promotion where customers receive a free item when their cart total reaches a certain amount.


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