Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Add a Free Product Based on Time & Date Range in WooCommerce?

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;
}

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.

How to Add a Free Product Based on Time & Date Range in WooCommerce? - Tyche Softwares

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.

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