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) Offer on Holidays/Events in WooCommerce?

Special BOGO (Buy One Get One) offers on holidays/events can attract more customers to your store and ulimately it will drive shopping spikes. With the following WooCommerce customization, you can seamlessly integrate holiday-specific promotions into your online store, making each festive occasion more impactful.

Solution: Add BOGO (Buy One Get One) Offer on Holidays/Events in WooCommerce

This code will offer free gifts only on the holiday dates that are specified in the code. Also, it checks for upcoming holidays and displays a notice if there is a holiday within a certain range (e.g., 7 days) from the current date.

 // Function to get the list of holidays for a given year
function ts_getHolidays($year) {
    return array("2024-02-07", "2024-02-14", "2024-04-01", "2024-05-01", "2024-12-25");
}

// Function to check for upcoming holidays and display a notice
function ts_checkUpcomingHolidays() {
    $currentDate = new DateTime();
    $currentYear = $currentDate->format('Y');
    $upcomingHolidays = ts_getHolidays($currentYear);

    foreach ($upcomingHolidays as $holiday) {
        $holidayDate = new DateTime($holiday);

        // Check if the holiday date is in the future
        if ($holidayDate > $currentDate) {
            // Check if the holiday date is within a certain range from the current date (e.g., 7 days)
            $dateDifference = $currentDate->diff($holidayDate)->days;

            // Display a notice for the upcoming event
            if ($dateDifference >= 0 && $dateDifference <= 7) {
                $event_name = "BOGO Offer"; // Replace with the actual event name
                $notice_message = "$event_name is available on the upcoming event $holiday.";
                wc_add_notice($notice_message, 'success');
                break; // Stop processing after the first upcoming event is found
            }
        }
    }
}

// Hook to display notice message
add_action('woocommerce_before_main_content', 'ts_checkUpcomingHolidays');

// Hook to add a free product to the cart if a product is selected on any holiday
function ts_addFreeProductOnHoliday($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {
    $currentDate = new DateTime();
    $currentYear = $currentDate->format('Y');
    $upcomingHolidays = ts_getHolidays($currentYear);

    $free_product_id = 470; // Replace with your actual free product ID

    // Check if the current date is a holiday
    if (in_array($currentDate->format('Y-m-d'), $upcomingHolidays)) {
        // Check if the free product is 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 the free product is not found in the cart, add it
        if (!$found) {
            WC()->cart->add_to_cart($free_product_id);
        }
    }
}

// Hook to add a free product to the cart when a product is added
add_action('woocommerce_add_to_cart', 'ts_addFreeProductOnHoliday', 10, 6);
flexi bogo cta banner image


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

When the customer visits the site on particular holidays that are specified in the code and proceeds to add an item to the cart, the free gift also gets automatically added to the cart.

BOGO (Buy One Get One) Offer on Holidays/Events

Additionally, if there is an upcoming holiday within a specified date range, a notice message is also displayed on the shop page to inform the user about the upcoming offer.

How to Add BOGO (Buy One Get One) Offer on Holidays/Events in WooCommerce? - Tyche Softwares

Similarly, you can also implement additional holiday promotions that are both simple and effective by adding bogo buy one get one offers with time limit in WooCommerce.

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

Share It:

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