2019 年更新:你应该尝试这种更短、紧凑、有效的方法:
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 class to find
$class = 92;
// HERE define the shipping method to hide
$method_key_id = 'flat_rate:7';
// Checking in cart items
foreach( $package['contents'] as $item ){
// If we find the shipping class
if( $item['data']->get_shipping_class_id() == $class ){
unset($rates[$method_key_id]); // Remove the targeted method
break; // Stop the loop
}
}
return $rates;
}
代码位于活动子主题(或主题)的 function.php 文件中或任何插件文件中。
经过测试并有效。
有时,您可能需要刷新前往送货区域的送货方式,然后禁用/保存并重新启用/保存您的“统一费率”送货方式。
Related: 在 WooCommerce 中隐藏特定运输类别的运输方式
要查找运输方式 ID 和运输类别 ID,请参见下文...
许多不同运输方式的更新(与您的评论相关):
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 class to find
$class = 92;
// HERE define the shipping methods you want to hide
$method_key_ids = array('flat_rate:7', 'local_pickup:3');
// Checking in cart items
foreach( $package['contents'] as $item ) {
// If we find the shipping class
if( $item['data']->get_shipping_class_id() == $class ){
foreach( $method_key_ids as $method_key_id ){
unset($rates[$method_key_id]); // Remove the targeted methods
}
break; // Stop the loop
}
}
return $rates;
}
经过测试并有效...
查找运输类别 ID。
- 在数据库下
wp_terms
table:
搜索术语名称或术语别名,您将获得术语 ID(运输类别 ID)。
- 在 Woocommerce 运输设置中编辑“统一费率”,使用浏览器 html 检查器工具检查运输类别费率字段,例如:
在输入名称属性中,您有woocommerce_flat_rate_class_cost_64
。因此 64 是运输类别的 ID。
获取送货方式费率 ID:
来获取相关的运输方式费率 ID, 就像是flat_rate:12
,使用浏览器代码检查器检查每个相关的单选按钮属性value
like: