This customization will help store owners to set a time limit for BOGO offers that will encourage newly registered customers to go ahead and make a purchase.
The code helps to check if the user is eligible for the BOGO offer based on their registration time. It calculates the ending time for the offer by adding a specified time limit (3 hours in this case) to the user’s registration time. If the current time is within this time limit, the user is considered eligible to avail the free product. Also, a notice message is displayed on the shop page for communicating such time-limited offers.
// Function to get the registration time for a user function ts_get_user_registration_time($user_id) { return get_user_meta($user_id, 'registration_time', true); } // Function to check if the user is eligible for the BOGO offer function ts_is_bogo_offer_time_for_user($user_id) { $registration_time = ts_get_user_registration_time($user_id); if (!$registration_time) { return false; // Return false if registration time is not available } // Define the time limit (3 hours in this case) $time_limit = 3 * 60 * 60; // Calculate the ending time based on registration time and time limit $ending_time = $registration_time + $time_limit; // Check if the current time is within the specified time limit from the registration time return (current_time('timestamp') <= $ending_time); } // Action to add free product during BOGO offer time for registered users add_action('woocommerce_before_cart', 'ts_bogo_daily_time_limit_for_user'); add_action('woocommerce_checkout_update_order_review', 'ts_bogo_daily_time_limit_for_user'); // Action to display notice on the shop page during BOGO offer time for registered users add_action('wp', 'ts_display_bogo_notice_on_shop_page_for_user'); function ts_display_bogo_notice_on_shop_page_for_user() { $user_id = get_current_user_id(); // Check if the user is on the shop page and is eligible for the BOGO offer if (is_shop() && is_user_logged_in() && ts_is_bogo_offer_time_for_user($user_id)) { // Check if the notice has already been displayed if (!get_user_meta($user_id, 'bogo_notice_displayed', true)) { // Calculate remaining time $remaining_time = ts_get_user_registration_time($user_id) + (3 * 60 * 60) - current_time('timestamp'); 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'); // Set a flag indicating that the notice has been displayed update_user_meta($user_id, 'bogo_notice_displayed', true); } } } } function ts_bogo_daily_time_limit_for_user() { $user_id = get_current_user_id(); if (is_user_logged_in() && ts_is_bogo_offer_time_for_user($user_id)) { // BOGO offer is active during this time for the registered user // 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 for the registered user return false; } } function ts_set_registration_time_on_registration($user_id) { $registration_time = current_time('timestamp'); update_user_meta($user_id, 'registration_time', $registration_time); } // Hook to set the registration time when a new user registers add_action('user_register', 'ts_set_registration_time_on_registration');
Output
Whenever new registrants visit the shop page, they will be notified that the offer is available for a limited time.
And, if the newly registered user tries to add a product to the cart, the free product will also get added to the cart automatically. If the offer limit has been exceeded the free product will not be added.
Similar to the above functionality, you can also add a free product based on time and date range in WooCommerce.