Cut off times

Some WooCommerce shop owners require cut off times for delivery times during their checkout process. By that for example, a store owner can specify a time when next day deliveries are no longer possible. Maybe because of packing times or other various reasons. When using our WooCommerce Delivery plugin, cut off times are possible by using the hook “woocommerce_delivery_time_slots_ajax_options”. Below you can see an example code:

add_filter('woocommerce_delivery_time_slots_ajax_options', function($deliveryTimeOptions, $selectedDateObject, $currentDate) {

    $cutOffTime = '09:00';
    $tomorrowDate = new DateTime('tomorrow');

    $tomorrowDateFormatted = $tomorrowDate->format('Y-m-d');
    $selectedDateFormatted = $selectedDateObject->format('Y-m-d');

    if($selectedDateFormatted == $tomorrowDateFormatted) {

        $currentDateTimeCutOff = new DateTime($cutOffTime);
        if($currentDate >= $currentDateTimeCutOff) {
            $deliveryTimeOptions = array(
                '' => '09 am cut off. Please select the next date.'
            );
        }

    }

    return $deliveryTimeOptions;
    

}, 20, 3);

Add this snippet into your child theme functions.php. It will cut off next day delivery times, when the current user time exceeds 9 am. Change 09:00 to your required time and modify the response text to your needs.

Cut off dates

To disable dates you can use one of these two filters:

woocommerce_delivery_disabled_dates
woocommerce_delivery_enabled_dates

An example to disable delivery dates: Disable Friday, if current week wednesday 14:00 pm has passed

add_filter('woocommerce_delivery_disabled_dates', function($disabledDates) {

    $now = new DateTime();
    $today = new DateTime();
    
    // Ensure 'this Wednesday' refers to the current week's Wednesday
    $wednesdayCutoff = new DateTime('Wednesday this week 14:00');
    // $wednesdayCutoff = new DateTime('Tuesday this week 14:00');
    $friday = new DateTime('Friday this week');
    
    // If today is Wednesday and it's past 2 PM, or if today is after Wednesday, disable Friday
    if ($now > $wednesdayCutoff) {
        $disabledDates[] = $friday->format('Y-m-d');;
    }

    return $disabledDates;

}, 10, 1);

An example to enabled delivery dates specifically. We use the enabledDates and remove all enabled dates past the current day + 2 days.


add_filter('woocommerce_delivery_enabled_dates', function($enabledDates) {

	date_default_timezone_set('Asia/Jerusalem'); // Set timezone to Israel

	$now = new DateTime();

    sort($enabledDates); // Ensure dates are in ascending order
    $filteredEnabledDates = [];

    foreach ($enabledDates as $date) {
        $availableDate = new DateTime($date . ' 14:00');
        $cutoff = clone $availableDate;
        $cutoff->modify('-2 days');
        
        if ($now <= $cutoff) {
            $filteredEnabledDates[] = $availableDate->format('Y-m-d');
        }
    }

    return $filteredEnabledDates;


}, 10, 1);

6 thoughts on “WooCommerce Delivery Cut Off Times & Dates

  1. coclico says:

    Hello,
    i am using this example, just above. I just change $cutOffTime with 16h00 … and it doesn’t work. Nothing happens when changing date / hours.
    Something to fix ?
    Thanks !

  2. stephanie says:

    hello i using this example, just above. I just change $cutOffTime with 12h00 … and it doesn’t work. Nothing happens when changing date / hours.
    Something to fix ?

  3. ker xin says:

    I have tried insert the cut off time code to my functions.php, but when I want to save it, it shows “Something went wrong. Your change may not have been saved. Please try again. There is also a chance that you may need to manually fix and upload the file over http://FTP.” I need help with this ya!

Leave a Reply

Your email address will not be published. Required fields are marked *