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

How to Add Free Gift based on Cart Quantity in WooCommerce?

This WooCommerce Buy One Get One customization will give customers the thrill of earning different free products based on their cart quantity! This is how it works! For instance, if the cart quantity is between 3 and 6, the code will add the first free product and display a notice encouraging customers to buy a bit more to unlock even more rewards.

On the other hand, if the cart quantity reaches between 8 and 12, the second free product will be automatically added to their cart. This way, you’re motivating customers to buy more, and by this, you can increase the average order value.

Solution: Add Free Gift based on Cart Quantity in WooCommerce

This code dynamically manages the addition and removal of free products based on cart quantity.

add_action('template_redirect', 'ts_add_product_to_cart');
function ts_add_product_to_cart() {
    if (!is_admin()) {
        global $woocommerce;
      
        $free_product_1_id = 470; // Replace with your free product 1 id
        $free_product_2_id = 54; // Replace with your free product 2 id
        $cart_quantity = $woocommerce->cart->cart_contents_count;

        // Remove free product 1 if already in the cart
        ts_remove_free_product($free_product_1_id);

        // Remove free product 2 if already in the cart
        ts_remove_free_product($free_product_2_id);

        // Check if cart quantity is between 3 and 6
        if ($cart_quantity >= 3 && $cart_quantity <= 6) {
            // Get the product name for Free Product 1
            $free_product_2_name = get_the_title($free_product_2_id);

             wc_add_notice("If you select beyond 6, you will get $free_product_2_name for free.", 'notice');

            // Add Free Product 1
            ts_add_free_product($free_product_1_id);
        } elseif ($cart_quantity >= 8 && $cart_quantity <= 12) {
            // Add Free Product 2
            ts_add_free_product($free_product_2_id);
        }
    }
}

function ts_add_free_product($free_product_id) {
    global $woocommerce;

    // Check if free product not already in cart
    if (!is_free_product_in_cart($free_product_id)) {
        $woocommerce->cart->add_to_cart($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);
        }
    }
}

function is_free_product_in_cart($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) {
            return true;
        }
    }

    return false;
}

Output

Based on the cart quantity of the selected product, the code adds a free product under certain conditions:

  • If the cart quantity is between 3 and 6, it adds the first free product ($free_product_1_id) and also displays a notice message as shown in the image below.
Add Free Gift based on Selected Quantity in WooCommerce?

If the cart quantity is between 8 and 12, it adds the second free product ($free_product_2_id).

Add Free Gift based on Selected Quantity in WooCommerce?

BBOGO offers tied to cart totals not only add excitement for your customers but also boost your sales. Similarly, you can also boost average order value by adding BOGO (Buy One Get One) offers based on WooCommerce cart total range

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