对注销用户隐藏多个Woocommerce发货方法



嗨,我正试图隐藏多个运输方法从注销用户和某些原因我的代码不工作,所以请帮助,这里是我的代码

add_filter( 'woocommerce_package_rates', 'shipping_methods_based_on_wholesale_customer', 10, 2 );
function shipping_methods_based_on_user_status( $rates, $package ){
$is_loggedout = !is_user_logged_in());

// Set the shipping methods rate ids in the arrays:
if( $is_loggedout ) {

} else {
$shipping_rates_ids = array('flat_rate:10', 'flat_rate:11', 'flat_rate:12', 'flat_rate:13'); // To be removed for loggedout users
}
// Loop through shipping rates fro the current shipping package
foreach( $rates as $rate_key => $rate ) {
if ( in_array( $rate_key, $shipping_rates_ids) ) {
unset( $rates[$rate_key] ); 
}
}

return $rates;
} 

您错过了条件,您的代码现在正在检查登录用户。代码应该如下所示:

add_filter( 'woocommerce_package_rates', 'shipping_methods_based_on_wholesale_customer', 10, 2 );
function shipping_methods_based_on_user_status( $rates, $package ){
// Check if logged out ( not logged in )
if( !is_user_logged_in() ) {

// Set the shipping methods rate ids in the arrays:
$shipping_rates_ids = array('flat_rate:10', 'flat_rate:11', 'flat_rate:12', 'flat_rate:13'); // To be removed for loggedout users

// Loop through shipping rates fro the current shipping package
foreach( $rates as $rate_key => $rate ) {
if ( in_array( $rate_key, $shipping_rates_ids) ) {
unset( $rates[$rate_key] ); 
}
}
}

return $rates;
} 

(未测试)

最新更新