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

How to Add a Free Gift When Any Coupon is Applied in WooCommerce?

While doing online shopping, every customer loves a good deal. Imagine the excitement your customers feel when they not only save money but also receive an unexpected gift. Yes, you got it right! In this guide, we’ll walk you through the steps to seamlessly add a gift when a coupon is applied in WooCommerce.

This double offering—a discount paired with a gift—is more than usual shopping. Crafting such moments of joy in their shopping experience will keep them coming back, and bring new traffic and potential buyers to your store.

Solution: Add a Free Gift When Any Coupon is Applied in WooCommerce

This code snippet adds a specific product (identified by its ID given in the code) as a free gift to the WooCommerce cart whenever any coupon is applied. It ensures that the free product is only added once per session, preventing duplicates in the cart.

function ts_add_free_product_based_on_coupon() {
    global $woocommerce;

    // Set the ID of the free product
    $free_product_id = 470; // Set the ID of the free product

    // Check if any coupon is applied
    if ($woocommerce->cart->applied_coupons) {

        // Check if the free product is not already in the cart
        $product_already_in_cart = false;
        foreach ($woocommerce->cart->get_cart() as $cart_item) {
            if ($cart_item['product_id'] == $free_product_id) {
                $product_already_in_cart = true;
                break;
            }
        }

        // If the free product is not in the cart, add it
        if (!$product_already_in_cart) {
            $woocommerce->cart->add_to_cart($free_product_id);
            wc_add_notice(__('Free product added!'), 'success');
        }
    }
}
add_action('woocommerce_before_cart_table', 'ts_add_free_product_based_on_coupon', 10, 0);

Output

Whenever a coupon is applied to the cart, the free product gets automatically added to the cart as shown below.

How to Add a Free Gift When Any Coupon is Applied in WooCommerce? - Tyche Softwares

In case you want to provide just the BOGO free gift you can disable the coupon field when specific BOGO promotions are offered in your WooCommerce store.

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