在周末禁用 woocommerce 交付方式并在一周内切断时间

Disable a woocommerce delivery method on weekends and cut off time in the week

根据 答案代码,我希望只允许周一至周五下午 3 点之前。

//hide shipping method based on time
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 = 'flat_rate:29';

    // When this shipping method is available and after 2 PM
    if ( array_key_exists( $shipping_rate_id, $rates ) && date('H') > 14 ) {
        unset($rates[$shipping_rate_id]); // remove it
    }
    return $rates;

}

您将使用带有 N 参数的 date() 函数来处理星期几,并使用 H 参数来处理时间,如下所示,以在周末禁用特定的送货方式,工作日下午 3 点后:

// Hide shipping method based on the day of the week and time
add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_day_of_the_week_and_time', 10, 2 );
function hide_shipping_method_based_on_day_of_the_week_and_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 = 'flat_rate:29';

    // When this shipping method is available and after 2 PM
    if ( array_key_exists( $shipping_rate_id, $rates ) 
    && ! ( date('H') <= 15 && ! in_array(date('N'), [6,7]) ) ) {
        unset($rates[$shipping_rate_id]); // remove it
    }
    return $rates;
}

代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。

Clearing shipping caches:

  • You will need to empty your cart, to clear cached shipping data
  • Or In shipping settings, you can disable / save any shipping method, then enable back / save.