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

How to Buy Product A and Product B with a Total Minimum Quantity and Get a Free Product C in WooCommerce?

If you are selling wholesale items in your online store, then at some point you will be using some BOGO offers that help to drive sales. This code snippet will offer a free product C when purchasing a combination of products A and B with a total minimum quantity of 5.

Let’s consider an example scenario where the store sells the latest smart home devices. If a customer purchases smart light bulbs (Product A) and smart plugs (Product B) from an electronics retailer. The promotion indicates that if you acquire a minimum of 5 items, combining both the smart bulbs and plugs as needed, you’ll receive a voice-controlled virtual assistant device (Product C) at no cost.

add_action('template_redirect', 'ts_add_gift_id_in_cart');

function ts_add_gift_id_in_cart() {
    if (is_admin()) return;
    if (WC()->cart->is_empty()) return;

    $product_a_id = 100;
    $product_b_id = 911;
    $product_gifted_id = 470;
    $min_combined_qty_required = 5;

    // Get quantities of Product A and Product B in cart
    $qty_product_a = WC()->cart->get_cart_item_quantities()[$product_a_id];
    $qty_product_b = WC()->cart->get_cart_item_quantities()[$product_b_id];

    // Calculate the combined quantity
    $combined_qty = $qty_product_a + $qty_product_b;

    // Check if the combined quantity meets the minimum requirement
    if ($combined_qty >= $min_combined_qty_required) {
        // Check if the free product is not already in the cart
        $product_gifted_cart_id = WC()->cart->generate_cart_id($product_gifted_id);
        $product_gifted_in_cart = WC()->cart->find_product_in_cart($product_gifted_cart_id);

        // If the free product is not in the cart, add it
        if (!$product_gifted_in_cart) {
            WC()->cart->add_to_cart($product_gifted_id);
        }
    } else {
        // If the minimum combined quantity requirement is not met, remove the free product if present
        $product_gifted_in_cart = WC()->cart->find_product_in_cart($product_gifted_id);
        if ($product_gifted_in_cart) {
            WC()->cart->remove_cart_item($product_gifted_in_cart);
        }
    }
}

Output

When a customer buys smart home devices in bulk quantities and if the quantity of those specified products meet the threshold of the minimum combined quantity (A+B), the code automatically adds the complimentary product ‘smart speaker ‘ to the cart.

How to Buy Product A and Product B with a Total Minimum Quantity and Get a Free Product C in WooCommerce? - Tyche Softwares

Until the combined quantity threshold of product A and Product B is not met, the free Product C will not be added to the cart as shown below.

How to Buy Product A and Product B with a Total Minimum Quantity and Get a Free Product C in WooCommerce? - Tyche Softwares

Alternatively, you can also fix BOGO offers based on individual product quantity as well. This post will help you to add a BOGO offer for a fixed quantity in WooCommerce.


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