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

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

For advertising and selling new products, you can also offer BOGO like 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.

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) {
                } else {
                    // If the quantity condition is not met, remove the free product

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


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:

Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x