This customization is really handy when giving Buy One Get One offers based on the ranges of order total. Regular offers usually stick to a fixed total amount which lacks some flexibility. But with this code, it’s almost like having discount tiers. You can now manage promotions between higher-value and low-value products and set criteria for when customers spend a specific amount.
Solution: Add BOGO (Buy One Get One) Gifts Based on Order Total Range
This code snippet will automatically add two different free products to a customer’s cart based on the range of total order amount.
For instance, if a customer’s order total falls between $200 to $300, they might get a low-value product for free. On the other hand, if customers spend between $500 and $1000, they might get a high-value product for free.
add_action('template_redirect', 'ts_add_product_to_cart'); function ts_add_product_to_cart() { if (!is_admin()) { global $woocommerce; $free_product_id_1 = 470; // Product ID for the first range $free_product_id_2 = 457; // Product ID for the second range $found = false; $min_order_total_1 = 200; // Minimum order total needed to add the first free product $max_order_total_1 = 300; // Maximum order total needed to add the first free product $min_order_total_2 = 500; // Minimum order total needed to add the second free product $max_order_total_2 = 1000; // Maximum order total needed to add the second free product $order_total = $woocommerce->cart->total; // Check if the order total is within the specified range for the first free product if ($order_total >= $min_order_total_1 && $order_total <= $max_order_total_1) { add_free_product_to_cart($free_product_id_1); } // Check if the order total is within the specified range for the second free product elseif ($order_total >= $min_order_total_2 && $order_total <= $max_order_total_2) { add_free_product_to_cart($free_product_id_2); } } } // Function to add a free product to the cart function add_free_product_to_cart($free_product_id) { global $woocommerce; // Check if the product is already in the cart $found = false; if (sizeof($woocommerce->cart->get_cart()) > 0) { foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) { $_product = $values['data']; if ($_product->get_id() == $free_product_id) { $found = true; } } // If the product is not found, add it if (!$found) { $woocommerce->cart->add_to_cart($free_product_id); } } else { // If no products in cart, add it $woocommerce->cart->add_to_cart($free_product_id); } }
This to the shop owners who are running or planning to run BOGO offers on their WooCommerce store…
BOGO deals are great for increasing your sales, but have you thought about which offers are bringing you more revenue and which offers are not performing that great?
Don’t just set a BOGO deal, track the revenue generated by your deals in real-time with the Flexi BOGO for WooCommerce plugin.
Output
If the order total is, for example, $215, the code checks the first range (between $200 and $300), and as it falls within this range the ‘free product 1’ is added to the cart.
If the order total is, for example, $884, the code checks the second range (between $200 and $300) and adds the corresponding ‘free product 2’ that matches this range of order total.
Similar to the above customization, you can also add different free gifts based on the cart total quantity. Checkout our guide on how to add free gift based on selected quantity in WooCommerce.