Avvia un progetto

Orari e date limite per le consegne WooCommerce

Orari limite

Alcuni negozi WooCommerce hanno bisogno di orari limite per le fasce di consegna al checkout. Ad esempio il negozio può indicare un orario dopo il quale la consegna il giorno dopo non è più possibile, per i tempi di imballaggio o altri motivi. Con il nostro plugin WooCommerce Delivery puoi farlo con l’hook “woocommerce_delivery_time_slots_ajax_options”. Ecco un esempio di codice:

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

Aggiungi questo snippet nel file functions.php del tuo child theme. Elimina le fasce di consegna del giorno dopo quando l’ora attuale dell’utente supera le 9. Cambia 09:00 con l’orario che ti serve e adatta il testo della risposta.

Date limite

Per disattivare delle date puoi usare uno di questi due filtri:

woocommerce_delivery_disabled_dates
woocommerce_delivery_enabled_dates

Esempio per disattivare date di consegna: disattiva il venerdì se è passato il mercoledì alle 14 della settimana in corso

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

Esempio per attivare date di consegna specifiche: usiamo enabledDates e rimuoviamo tutte le date attive oltre il giorno attuale + 2 giorni.

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