Woocommerce 在周末禁用电子商务配送方式,并在一周内切断时间

Woocommerce 在周末禁用电子商务配送方式,并在一周内切断时间,woocommerce,shipping,Woocommerce,Shipping,根据答案代码,我希望只允许周一到周五下午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

根据答案代码,我希望只允许周一到周五下午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;
}
//根据星期几和时间隐藏发货方法
添加过滤器('woocommerce\u package\u rates'、'hide\u shipping\u method\u基于\u周的\u day\u和\u time',10,2);
函数hide\u shipping\u method\u基于\u day\u的\u周和\u时间($rates,$package)
{
//设置默认时区(http://php.net/manual/en/timezones.php)
日期默认时区设置(“欧洲/伦敦”);
//这里设置您的运费Id
$shipping_rate_id='flat_rate:29';
//当这种运输方式可用时,下午2点后
如果(数组\u键\u存在($shipping\u rate\u id,$rates)

&&!(日期('H')非常感谢,我绞尽脑汁想了两天,突然发现了你的初始代码,关于(日期('N')6,7。我的商店设置从周一开始,这会对6,7有影响吗?条件
日期('H'))