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
|
1 Answer
Reset to default 0To 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
版权声明:本文标题:php - Hide specific shipping methode depending on day and time of day 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741493271a2381719.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
&
instead of&&
), fixed. – Ivan Shatsky Commented Nov 25, 2020 at 8:19