admin管理员组

文章数量:1290192

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

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/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;
}
Share Improve this question asked Nov 24, 2020 at 8:11 Jordy SteyaertJordy Steyaert 11 bronze badge 3
  • What is the saturday delivery option? The "vanilla" WooCommerce does not allow to choose delivery date/time for the order. – Ivan Shatsky Commented Nov 24, 2020 at 8:23
  • @Ivan Shatsky I have multiple delivery options they can choose from - like local pickup, delivery on saturday, delivery on sonday, etc. But I want to disable certain options based on day and time. The above snippet is only for time. – Jordy Steyaert Commented Nov 24, 2020 at 11:56
  • There was a typo in original answer (& instead of &&), fixed. – Ivan Shatsky Commented Nov 25, 2020 at 8:19
Add a comment  | 

1 Answer 1

Reset to default 0

To get the current day of week you can use the date('w') function call (complete format description can be found here. The whole condition to exclude the shipping method, say it is saturday and over 11 o'clock, can be written as

date('w') == 6 && date('H') > 11

and the whole if block would be

    if ( array_key_exists( $shipping_rate_id, $rates ) && date('w') == 6 && date('H') > 11 ) {
        unset($rates[$shipping_rate_id]); // remove the shipping method
    }

You are using the 'local_pickup:13' shipping method id as shown in the example you are referring to, but you'd need to find an actual shipping method id for your particular case.

本文标签: phpHide specific shipping methode depending on day and time of day