在 WooCommerce 中,我使用“隐藏 WooCommerce 中特定运输类别的运输方式”答案代码(第二种方式)在购物车中隐藏基于不同运输类别的运输方式,但问题是我使用管理 2 种语言站点的 WPML 插件,所以只找一个班是行不通的。
所以我需要处理 2 个运输类而不是一个。我尝试以这种方式添加 2 个运输类:
// HERE define your shipping classes to find
$class = 3031, 3032;
但它破坏了网站。所以我想隐藏定义的统一费率,不仅适用于运输类别3031
和3032
.
我做错了什么?如何在不破坏网站的情况下启用 2 个运输类别?
要使用多个运输类,您应该首先在一个数组中定义它们,并在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 文件中。测试和工作。
有时,您可能需要刷新前往配送区域的配送方式,然后禁用/保存并重新启用/保存您的“统一费率”配送方式。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句