In our previous post, we have discussed on how to add a free product when a specific coupon is applied based on cart total. In this post we will cover how to add a free gift when any coupon gets applied. This feature encourages customers to explore and redeem available coupons potentially attracting more customers and driving sales as well.
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.
Alternatively, you can remove the coupon field when specific BOGO promotions are running on your store. Check out this guide that will help you disable coupons when a specific bogo buy one get one is offered in WooCommerce.