如何从一周中的某些天限制 WooCommerce 送货区
How to restrict WooCommerce shipping zone from certain days of the week
我需要在周日隐藏 WooCommerce 运费 zone_id=1 和运费 zone_id=3。
我偶然发现了 this answer,并想出了这段代码:
// Hide & shipping rates on Sunday
add_filter('woocommerce_package_rates', 'amano_hide_shipping_method_on_sunday', 10, 2);
function amano_hide_shipping_method_on_sunday($available_shipping_methods, $package) {
$date = date("1");
$is_sunday = date('l', $date) == 'Sunday';
if($is_sunday) {
$hide_when_shipping_class_exist = array(
42 => array(
'free_shipping'
)
);
$hide_when_shipping_class_not_exist = array(
42 => array(
'wf_shipping_ups:03',
'wf_shipping_ups:02',
'wf_shipping_ups:01'
),
43 => array(
'free_shipping'
)
);
$shipping_class_in_cart = array();
foreach(WC()->cart->cart_contents as $key => $values) {
$shipping_class_in_cart[] = $values['data']->get_shipping_class_id();
}
foreach($hide_when_shipping_class_exist as $class_id => $methods) {
if(in_array($class_id, $shipping_class_in_cart)){
foreach($methods as & $current_method) {
unset($available_shipping_methods[$current_method]);
}
}
}
foreach($hide_when_shipping_class_not_exist as $class_id => $methods) {
if(!in_array($class_id, $shipping_class_in_cart)){
foreach($methods as & $current_method) {
unset($available_shipping_methods[$current_method]);
}
}
}
return $available_shipping_methods;
}
}
但是,我不知道如何定制的是:
$hide_when_shipping_class_exist = array(
42 => array(
'free_shipping'
)
);
$hide_when_shipping_class_not_exist = array(
42 => array(
'wf_shipping_ups:03',
'wf_shipping_ups:02',
'wf_shipping_ups:01'
),
43 => array(
'free_shipping'
)
);
我没有任何运费 类,也不明白它们是什么。可能需要替换更多代码以用隐藏运输 类 代替运输区域,但我不知道该代码是什么。
请多多帮助。
代码包含
- 在特定的一天
- 为某些区域 ID 取消设置
$rates[$rate_key]
在代码中添加解释的评论
function filter_woocommerce_package_rates( $rates, $package ) {
/* SETTINGS */
// Zone ID's
$hide_zones = array ( 1, 3 );
$on_day = 'Sunday';
/* END SETTINGS */
// Shipping zone
$shipping_zone = wc_get_shipping_zone( $package );
// Get zone ID
$zone_id = $shipping_zone->get_id();
// set the default timezone to use. Available since PHP 5.1
date_default_timezone_set('UTC');
// l - A full textual representation of the day of the week
$day_of_the_week = date( 'l' );
// True
if( $day_of_the_week == $on_day ) {
// In array
if ( in_array( $zone_id, $hide_zones ) ) {
// Loop
foreach ( $rates as $rate_key => $rate ) {
// Unset
unset( $rates[$rate_key] );
}
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'filter_woocommerce_package_rates', 10, 2 );
我需要在周日隐藏 WooCommerce 运费 zone_id=1 和运费 zone_id=3。
我偶然发现了 this answer,并想出了这段代码:
// Hide & shipping rates on Sunday
add_filter('woocommerce_package_rates', 'amano_hide_shipping_method_on_sunday', 10, 2);
function amano_hide_shipping_method_on_sunday($available_shipping_methods, $package) {
$date = date("1");
$is_sunday = date('l', $date) == 'Sunday';
if($is_sunday) {
$hide_when_shipping_class_exist = array(
42 => array(
'free_shipping'
)
);
$hide_when_shipping_class_not_exist = array(
42 => array(
'wf_shipping_ups:03',
'wf_shipping_ups:02',
'wf_shipping_ups:01'
),
43 => array(
'free_shipping'
)
);
$shipping_class_in_cart = array();
foreach(WC()->cart->cart_contents as $key => $values) {
$shipping_class_in_cart[] = $values['data']->get_shipping_class_id();
}
foreach($hide_when_shipping_class_exist as $class_id => $methods) {
if(in_array($class_id, $shipping_class_in_cart)){
foreach($methods as & $current_method) {
unset($available_shipping_methods[$current_method]);
}
}
}
foreach($hide_when_shipping_class_not_exist as $class_id => $methods) {
if(!in_array($class_id, $shipping_class_in_cart)){
foreach($methods as & $current_method) {
unset($available_shipping_methods[$current_method]);
}
}
}
return $available_shipping_methods;
}
}
但是,我不知道如何定制的是:
$hide_when_shipping_class_exist = array(
42 => array(
'free_shipping'
)
);
$hide_when_shipping_class_not_exist = array(
42 => array(
'wf_shipping_ups:03',
'wf_shipping_ups:02',
'wf_shipping_ups:01'
),
43 => array(
'free_shipping'
)
);
我没有任何运费 类,也不明白它们是什么。可能需要替换更多代码以用隐藏运输 类 代替运输区域,但我不知道该代码是什么。
请多多帮助。
代码包含
- 在特定的一天
- 为某些区域 ID 取消设置
$rates[$rate_key]
在代码中添加解释的评论
function filter_woocommerce_package_rates( $rates, $package ) {
/* SETTINGS */
// Zone ID's
$hide_zones = array ( 1, 3 );
$on_day = 'Sunday';
/* END SETTINGS */
// Shipping zone
$shipping_zone = wc_get_shipping_zone( $package );
// Get zone ID
$zone_id = $shipping_zone->get_id();
// set the default timezone to use. Available since PHP 5.1
date_default_timezone_set('UTC');
// l - A full textual representation of the day of the week
$day_of_the_week = date( 'l' );
// True
if( $day_of_the_week == $on_day ) {
// In array
if ( in_array( $zone_id, $hide_zones ) ) {
// Loop
foreach ( $rates as $rate_key => $rate ) {
// Unset
unset( $rates[$rate_key] );
}
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'filter_woocommerce_package_rates', 10, 2 );