The customization in the provided code offers a specific benefit while providing BOGO “Buy One Get One” offers to customers when the following two conditions are met:
- The BOGO offer is triggered when customers apply a specific coupon code.
- Also, if the total order amount is equal to or exceeds $100. If any of the condition is not met then the free product is not added to the cart.
function ts_add_free_product_based_on_coupon() { global $woocommerce; $coupon_code = 'newyear2024'; // Set the coupon code to trigger free product $free_product_id = 470; // Set the ID of the free product $min_cart_total = 100; // Set the minimum cart total to trigger free product // Check if the coupon is applied and the cart total is above $100 if ($woocommerce->cart->has_discount($coupon_code) && $woocommerce->cart->subtotal >= $min_cart_total) { // 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
The output shows that the free product is added only when the coupon is applied and the cart subtotal is greater than the minimum cart total amount set in the code.
Similar to this, you can apply BOGO offers based on many conditions that are well explained in our post. Check on how to how to automatically add a product to your WooCommerce cart in 6 different conditions such as categories, cart total, add product on website visit, etc.