本质上,当购物车商品的运送类别为“Roller”(ID 92
).
这是我试过的代码:
add_filter('woocommerce_package_rates', 'wf_hide_shipping_method_based_on_shipping_class', 10, 2);
function wf_hide_shipping_method_based_on_shipping_class($available_shipping_methods, $package)
{
$hide_when_shipping_class_exist = array(
92 => array(
'flat_rate:7'
)
);
$shipping_class_in_cart = array();
foreach(WC()->cart->cart_contents as $key => $values) {
$shipping_class_in_cart[] = $values['data']->get_shipping_class_id();
}
foreach($hide_when_shipping_class_exist as $class_id => $methods) {
if(in_array($class_id, $shipping_class_in_cart)){
foreach($methods as & $current_method) {
unset($available_shipping_methods[$current_method]);
}
}
}
return $available_shipping_methods;
}
航运类别 ID 92
是航运类别,我想为其隐藏 flat_rate:7
。
我的网站是这样的:http://www.minimoto.me/ WordPress:4.8.4 WooCommerce:3.1.1
任何帮助将不胜感激。
最佳答案
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 文件中或任何插件文件中。
经过测试并有效。
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.
相关: Hide shipping methods for specific shipping classes in WooCommerce
To find the shipping methods IDs and the shipping classes IDs see below…
许多不同运输方式的更新(与您的评论相关):
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
表下的数据库中:
搜索术语名称或术语 slug,您将获得术语 ID(发货类别 ID)。
- 在 Woocommerce 运费设置中编辑“统一费率”,使用浏览器 html 检查器工具,检查运费等级字段,例如:
在输入名称属性中,您有 woocommerce_flat_rate_class_cost_64
。所以 64 是航运类别的 ID。
获取送货方式费率 ID:
To get the related shipping methods rate IDs, something like
flat_rate:12
, inspect with your browser code inspector each related radio button attributevalue
like:
关于php - 在 WooCommerce 中隐藏特定运输类别的运输方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47718180/