Whether introducing a flash sale, a weekend special, or a holiday promotion, store owners are constantly seeking innovative strategies to boost sales. One effective method is implementing BOGO (Buy One, Get One) promotions based on specific dates and times. By aligning these promotions with peak shopping periods, you can maximize their impact and achieve better outcomes. In this guide, we will explore how to add a free product based on a time and date range in WooCommerce.
Solution: Add a Free Product Based on Time & Date Range in WooCommerce
The following code implements the functionality to add free product during the specified time period and also for every multiple of 3 items in the cart.
// Utility function that gives the free product during the discount period function ts_get_free_product_period() { date_default_timezone_set('Asia/Kolkata'); // Set the free product IDs $free_product_ids = array(470); // IDs of products eligible for free // Set the start time and the end time // Parameters: mktime(hour, minute, second, month, day, year) $start_time = mktime(11, 00, 00, date("01"), date("01"), date("2024")); $end_time = mktime(3, 00, 00, date("01"), date("20"), date("2024")); $time_now = strtotime("now"); // Return the array of free product IDs during the allowed period or an empty array outside the period return $start_time <= $time_now && $end_time > $time_now ? $free_product_ids : array(); } // Enable calculated free products during the discount period add_filter('woocommerce_cart_calculate_fees', 'ts_periodic_free_products', 99); function ts_periodic_free_products($cart) { $free_product_ids = ts_get_free_product_period(); // Check if the current cart total quantity is a multiple of 3 $cart_quantity = WC()->cart->get_cart_contents_count(); if ($cart_quantity % 3 === 0) { foreach ($free_product_ids as $free_product_id) { // Display a notice message wc_add_notice(__('Free product added to your cart!'), 'success'); // Add a fee to the cart $cart->add_fee(__('Free product fee'), 0, false, 'free_product_' . $free_product_id); // Add the free product to the cart WC()->cart->add_to_cart($free_product_id); } } } // Handling variation prices caching add_filter('woocommerce_get_variation_prices_hash', 'ts_add_free_product_to_variation_prices_hash', 99, 1); function ts_add_free_product_to_variation_prices_hash($hash) { if (!empty(ts_get_free_product_period())) { $hash[] = 'free_product_variation'; } return $hash; }
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
In the provided output, the free product is added when the current time & date falls within the specified time range in the code, and the product quantity is a multiple of 3. Additionally, a notice message is shown to inform the customer that the free gift has been added.
To enhance your WooCommerce store, you can consider exploring other innovative methods to apply the BOGO offer. There are various conditions under which you can automatically add products to a customer’s cart, such as based on cart total, product categories, or even website visits. Such advanced BOGO techniques can further optimize your sales strategies and draw in more customers as well.