更新时间:2023-11-30 11:54:46
要使用多个运输类,应首先在中定义它们一个数组,并在 IF
语句中,您将使用 in_array()
条件函数:
To use multiple shipping classes, you should first defined them in an array and in the IF
statement you will use in_array()
conditional function this way:
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文件中)。经过测试并可以正常工作。
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
有时,您可能需要刷新进入运输区域的运输方法,然后禁用/保存并重新启用/保存您的统一费率送货方式。
Sometimes, you should may be need to refresh shipping methods going to shipping areas, then disable / save and re-enable / save your "flat rates" shipping methods.