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

WooCommerce Discounts Based on Product Quantity: Buy Product A and Product B with a Total Minimum Quantity and Get a Free Product C

In this guide, we will explore a simple PHP snippet that helps you implement BOGO tactics to encourage bulk purchases and increase the average order value. This guide will assist you in offering a free product C when purchasing a combination of products A and B with a total minimum quantity of 5.

Solution: WooCommerce Discounts Based on Product Quantity

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);
        }
    }
}
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

ScenarioDiscounted/Free Product
Example
Buy Product A and Product B with Total Minimum Quantity Product C
Buy 5 units of smart light bulbs (Product A) and smart plugs (Product B) to get a free voice-controlled virtual assistant (Product C).
Discount Based on Quantity

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.

WooCommerce Discounts Based on Product Quantity: Buy Product A and Product B with a Total Minimum Quantity and Get a Free Product C - 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.

WooCommerce Discounts Based on Product Quantity: Buy Product A and Product B with a Total Minimum Quantity and Get a Free Product C - Tyche Softwares

If you’re interested in setting up a BOGO deal where a specific quantity of a single product triggers, then you can try this startegy 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
Newest
Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x