Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Set Up a BOGO Deal: Spend X More and Get a Free Product Based on Cart Amount in WooCommerce?

BOGO deals with a specific spending threshold will always encourage customers to spend a bit more in order to unlock the free product. Similarly, the below code snippet will help you to add the free product only when the specified minimum cart amount to attain the free product is met. On the other hand, the code also tends to display a notice message only when you are so close to meet the minimum amount for free product threshold such as ‘Spend £5 more and get a free T-shirt’.

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.

How to Set Up a BOGO Deal: Spend X More and Get a Free Product Based on Cart Amount in WooCommerce? - Tyche Softwares

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.

How to Set Up a BOGO Deal: Spend X More and Get a Free Product Based on Cart Amount in WooCommerce? - Tyche Softwares

Similarly, you can apply bogo offers based on ‘N’ number of conditions. Check out this guide that will help you to automatically add a free product to your WooCommerce cart in 6 different scenarios.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x