Hide specific shipping methode depending on day and time of day

Question

I would like to further expand the code given here: Hide specific shipping method depending on day time in Woocommerce

This snippet will, depending on the time of day, hide a specific shipping method, and it works. But I would also like to include the current day.

For example: get the current day and time, and if you are ordering on saturday (= today) after 11pm, you can no longer select the saturday delivery option. If ordering on saturday (= today) before 11pm, you can still select it.

add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_time', 10, 2 );
function hide_shipping_method_based_on_time( $rates, $package )
{
    // Set your default time zone (http://php.net/manual/en/timezones.php)
    date_default_timezone_set('Europe/London');
    
    // Here set your shipping rate Id
    $shipping_rate_id = 'local_pickup:13';

    // When this shipping method is available and after 11 AM
    if ( array_key_exists( $shipping_rate_id, $rates ) && date('H') > 11 ) {
        unset($rates[$shipping_rate_id]); // remove it
    }
    return $rates;
}
0
Jordy Steyaert 8 months 2020-11-24T03:10:24-05:00 0 Answers 12 views 0

Leave an answer

Browse
Browse