首页 > 解决方案 > 在 WooCommerce 中隐藏特定时间的运输方式

问题描述

我想在特定时间打开一些运输方式,比如

flat_rate:7将仅在 09.00 (24HR) 之前显示我尝试这个但不起作用

function conditional_hours_range(){
// HERE below, define start / end hours range and time zone (default is 'UTC').
$start_hour = 17;
$end_hour = 18;
date_default_timezone_set ('Asia/Bangkok');

$now = strtotime("now"); // Now time
$today_time = strtotime(date("Y-m-d")); // Today time at 00:00
$starting_time = $today_time + ( $start_hour * 3600 );
$ending_time = $today_time + ( $end_hour * 3600 );
// return true or false
return $now >= $starting_time && $now <= $ending_time ? false : true;}

add_filter( 'woocommerce_available_payment_gateways', 'hide_payment_gateways_based_on_weight', 11, 1 );
function hide_shipping_gateways_based_on_weight( $available_gateways ) {
if( conditional_hours_range() ){
        unset($method_key_id['wbs:1:a78cdfa1_1_09_00']);
         // unset 'cod'
}
}

标签: wordpresswoocommercehook-woocommerce

解决方案


在 function.php 中使用它

function remove_shipping_options_for_time($available_shipping_methods, $package) {
    global $woocommerce;
    if(YOUR_CONDITION){
        $available_shipping_methods = array();
    }
    return $available_shipping_methods;
}

推荐阅读