要使用多个运输类,您应该首先将它们定义在一个数组中,然后在IF
语句中您将in_array()
这样使用条件函数:
add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
function hide_shipping_method_based_on_shipping_class( $rates, $package )
{
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// HERE define your shipping classes to find
$classes = [3031, 3032];
// HERE define the shipping methods you want to hide
$method_key_ids = array('flat_rate:189');
// Checking in cart items
foreach( $package['contents'] as $item ) {
// If we find one of the shipping classes
if( in_array( $item['data']->get_shipping_class_id(), $classes ) ){
foreach( $method_key_ids as $method_key_id ){
unset($rates[$method_key_id]); // Remove the targeted methods
}
break; // Stop the loop
}
}
return $rates;
}
代码在您的活动子主题(或活动主题)的functions.php 文件中。测试和工作。
有时,您可能需要刷新前往配送区域的配送方式,然后禁用/保存并重新启用/保存您的“统一费率”配送方式。
相关主题:隐藏 WooCommerce 中特定运输类别的运输方式