This customization sets up a specific BOGO offer where customers receive a free product automatically added to their cart when they add another product, but only on a specific day of the week ‘Monday’ that is defined in the code as ((date(‘w’) == 1). Notices are displayed on product pages and during the checkout process to inform customers about the offer.
// Utility conditional function that checks if the day is Monday (returns boolean) function ts_is_monday() { // Set Your shop time zone (http://php.net/manual/en/timezones.php) date_default_timezone_set('America/New_York'); // If the current day is Monday, return true (else return false) return (date('w') == 1) ? true : false; } // Utility function (setting your free product ID) function ts_get_free_product_id() { return 470; // Change this to the ID of your free product } // Enable BOGO offer on Mondays add_filter('woocommerce_add_to_cart_validation', 'ts_enable_bogo_offer_on_mondays', 10, 3); function ts_enable_bogo_offer_on_mondays($passed, $product_id, $quantity) { // Enable BOGO offer only on Monday if (ts_is_monday()) { // Check if the product is not the free product itself if ($product_id !== ts_get_free_product_id()) { // Add the free product to the cart with a quantity of one WC()->cart->add_to_cart(ts_get_free_product_id(), 1); } } return $passed; } // Add a notice for free product on product page add_action('woocommerce_before_single_product', 'ts_filter_before_single_product'); function ts_filter_before_single_product() { global $product; if (ts_is_monday() && $product->get_id() !== ts_get_free_product_id()) { wc_print_notice(__('Get a free product with your purchase on Mondays!', 'woocommerce'), 'notice'); } } // Cart and checkout validation + error message add_action('woocommerce_check_cart_items', 'ts_conditionally_allowing_checkout'); add_action('woocommerce_checkout_process', 'ts_conditionally_allowing_checkout'); function ts_conditionally_allowing_checkout() { if (ts_is_monday()) { // Loop through cart items foreach (WC()->cart->get_cart() as $cart_item) { // Check cart items if ($cart_item['data']->get_id() === ts_get_free_product_id()) { // Set the quantity of the free product to one WC()->cart->set_quantity($cart_item['key'], 1); wc_add_notice(sprintf(__("You are eligible for a free product on Mondays."), "woocommerce"), 'notice'); break; } } } }
Output
When a customer visits the shop on ‘Monday’ and clicks on any product, notices indicating that the free product is exclusively available on Mondays are displayed on the product page of all products.
Subsequently, when the product is added to the cart, the free product gets added only on the specific day of the week i.e on Monday. To modify the day of the week on which free product should be offered, you should adjust the value of (date('w') == 1)
.
In similar to the free gifts offered on specific days of the week, you can also add a free gift based on time and date range in WooCommerce.