Do you want to provide BOGO ‘Buy one get one‘ offer to your users based on their order total? Well, then this code snippet will check the order total and if it meets the threshold amount set in the code, then the free product gets added to the cart.
add_action('template_redirect', 'ts_add_product_to_cart'); function ts_add_product_to_cart() { if (!is_admin()) { global $woocommerce; $product_id = 470; // Replace with your product id $found = false; $order_total = 200; // Replace with your order total needed to add the above item if ($woocommerce->cart->total >= $order_total) { // Check if the product is already in the cart if (sizeof($woocommerce->cart->get_cart()) > 0) { foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) { $_product = $values['data']; if ($_product->get_id() == $product_id) { $found = true; } } // If product not found, add it if (!$found) { $woocommerce->cart->add_to_cart($product_id); } } else { // If no products in cart, add it $woocommerce->cart->add_to_cart($product_id); } } } }
Output
The below output shows that when the cart total is greater than the threshold amount set in the code ( $order_total = 200), then the free gift is automatically added to the cart.
When the order total is below the threshold amount, the free product doesn’t get added to the cart.
Alternatively, you can also provide BOGO offers on different conditions. Refer to this post to check on 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.