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 during holidays or events can attract more customers to your store. 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);

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.

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

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 add 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
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x