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

How To Add BOGO (Buy One Get One) Offers with Time Limits in WooCommerce?

Limited-time BOGO offers always create a sense of urgency and notifying it explicitly will set your store apart from other stores. Also, customers may be more inclined to make a purchase to take advantage of the WooCommerce BOGO offer before it expires.

This post helps you to set a time limit for BOGO offers to be active between the specified time range(1:00 PM to 6:00 PM) and displays a notice on the shop page during the offer time. The code automatically adds a free product to the cart when any product is added within the specified time range.

// Conditional time function for BOGO offer
function ts_is_bogo_offer_time() {
    // HERE below, define start / end hours range and time zone (default is 'UTC').
    $start_hour = 13;
    $end_hour = 18;
    date_default_timezone_set('Asia/Kolkata');

    $now = strtotime("now"); // Current time
    $today_time = strtotime(date("Y-m-d")); // Today's time at 00:00
    $starting_time = $today_time + ($start_hour * 3600);
    $ending_time = $today_time + ($end_hour * 3600);

    // Return true if the current time is within the specified range (BOGO offer time)
    return $now >= $starting_time && $now <= $ending_time;
}

// Action to add free product during BOGO offer time
add_action('woocommerce_before_cart', 'ts_bogo_daily_time_limit');
add_action('woocommerce_checkout_update_order_review', 'ts_bogo_daily_time_limit');

// Action to display notice on the shop page during BOGO offer time
add_action('wp', 'ts_display_bogo_notice_on_shop_page');
function ts_display_bogo_notice_on_shop_page() {
    if (is_shop() && ts_is_bogo_offer_time()) {
        // Get the ending time
        $start_hour = 13;
        $end_hour = 18;
        date_default_timezone_set('Asia/Kolkata');
        $now = time();
        $today_time = strtotime(date("Y-m-d"));
        $ending_time = $today_time + ($end_hour * 3600);

        // Calculate remaining time
        $remaining_time = $ending_time - $now;
        if ($remaining_time > 0) {
            $remaining_hours = floor($remaining_time / 3600);
            $remaining_minutes = floor(($remaining_time % 3600) / 60);

            $message = sprintf('Hurry! BOGO offer ends in %d hours and %d minutes!', $remaining_hours, $remaining_minutes);

            wc_add_notice($message, 'notice');
        }
    }
}

function ts_bogo_daily_time_limit() {
    if (ts_is_bogo_offer_time()) {
        // BOGO offer is active during this time
        // Add your BOGO logic here (e.g., add free product to cart, display messages, etc.)

        // Add the free product directly to the cart when purchasing another product
        $free_product_id = 470; // Replace with the actual ID of your free product

        // Check if the free product is not already in the cart
        $found = false;
        foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
            $_product = $values['data'];
            if ($_product->get_id() == $free_product_id) {
                $found = true;
                break;
            }
        }

        // If free product not found, add it to the cart
        if (!$found) {
            WC()->cart->add_to_cart($free_product_id);
        }

        // Disable purchases during BOGO offer time
        return false;
    }
}

Output

In the below image, when customers visits the store between the timeframe 1:00 PM and 6:00 PM and add any product to the cart will automatically receive a free product ID(470). Also, a notice message about the remaining time for the BOGO offer is displayed on the shop page to make use of such offers.

How To Add BOGO (Buy One Get One) Offers with Time Limits in WooCommerce? - Tyche Softwares

On the shop page, it calculates the remaining time until the end of the BOGO offer and displays a notice message as shown below.

How To Add BOGO (Buy One Get One) Offers with Time Limits in WooCommerce? - Tyche Softwares

Similar to time-limit BOGO offers, you can also provide such offers that will automatically add a product to your WooCommerce cart based on many parameters such as cart total, categories, on website visit, etc.

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