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

How to Add a BOGO Offer for a Fixed Quantity in WooCommerce?

For advertising and selling new products, you can also offer BOGO promotions for buying an exact quantity. This can help to generate initial interest and sales for those products. Customers might be more willing to try a new product if there’s a special offer attached. Let’s consider an example of a store that sells coffee mugs. The store owner wants to encourage customers to buy a set of four coffee mugs, and for that specific quantity, they want to offer a special promotion.

Solution: Add a BOGO Offer for a Fixed Quantity in WooCommerce

The code snippet will automatically add a free gift to the cart when a customer buys a specific product in a fixed quantity of 4, as specified in the code.

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 for the parent product
        $targeted_qty_a_id = 4; // Targeted quantity

        // 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 meets the condition
                if ($cart_item['quantity'] == $targeted_qty_a_id) {
                    // 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 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);
        }
    }
}

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

When a customer adds the product ‘Coffee & Milk mugs’ to the cart in the quantity of 4 as specified in the code, then the free product is automatically added to the cart.

How to Add a BOGO Offer for a Fixed Quantity in WooCommerce? - Tyche Softwares

If the customer increases/ decreases the quantity of the parent product, and when the quantity of coffee mugs in the cart doesn’t meet the targeted quantity (4), the free product is dynamically removed from the cart.

How to Add a BOGO Offer for a Fixed Quantity in WooCommerce? - Tyche Softwares

Similarly, you can also add free products based on selected quantity in WooCommerce that will dynamically change the free products according to the selected quantity.

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