This WooCommerce snippet allows you to set up a “Spend X More to Get a Free Product“ promotion based on the cart total. In simple terms, it means, “Spend X more to unlock a free gift.” By creating a sense of urgency and excitement, this discount strategy effectively motivates customers to increase their cart value to receive a free product
Solution: Set Up a BOGO Deal: Spend X More and Get a Free Product
The code will automatically add a free product to the cart when a customer’s cart total spending reaches a specified amount. Additionally, it shows a message to encourage customers to spend a bit more to get the free item.
add_action('woocommerce_before_cart', 'ts_add_notice_and_free_product'); function ts_add_notice_and_free_product() { // Set the minimum amount for the free product $min_amount_for_free_product = 50; // Adjust this to your desired minimum amount $free_product_id = 470; // Replace with the actual product ID of the free product $cart_subtotal = WC()->cart->subtotal; // Use subtotal $remaining_amount = $min_amount_for_free_product - $cart_subtotal; // Set a threshold for displaying the notice $notice_threshold = $min_amount_for_free_product - 5; // If the cart subtotal is within the threshold, display the specific notice if ($cart_subtotal >= $notice_threshold && $cart_subtotal < $min_amount_for_free_product && $remaining_amount > 0) { $notice = sprintf("Spend %s more to get a free product!", wc_price($remaining_amount)); } elseif ($remaining_amount <= 0) { // If the threshold is reached, add the free product to the cart $product_to_add = wc_get_product($free_product_id); if ($product_to_add) { WC()->cart->add_to_cart($free_product_id); $added_notice = sprintf("Congratulations! You've earned a free %s. It has been added to your cart.", $product_to_add->get_name()); wc_print_notice($added_notice, 'success'); } } // Display the notice, if it is not empty if (!empty($notice)) { wc_print_notice($notice, 'notice'); } }
Output
If the customer’s cart total falls below the specified minimum amount ($50) required for the free product, and the cart total is close to the threshold amount ($5), the notice will be triggered, prompting the message ‘Spend $5 more to receive a free product.’
![](https://www.tychesoftwares.com/wp-content/uploads/2024/03/Notice-message-when-cart-subtotal-is-close-to-the-minimum-threshold-amount-for-free-product-1024x750.png)
When the customer’s cart subtotal has reached the specified minimum amount for free gift, then the free gift will be automatically added to the cart as shown below.
![](https://www.tychesoftwares.com/wp-content/uploads/2024/03/free-product-based-on-cart-total-1024x579.png)
Similarly, you can take this strategy a step further by offering a “Spend $X to unlock free shipping in WooCommerce” as this encourages customers to spend a bit more to benefit from free shipping.