问题描述
基本上,当存在具有运输类别"Roller"(ID 92
)的购物车商品时,我想尝试使统一费率方法ID flat_rate:7
已禁用.
Essentially I'm trying to make the flat rate method Id flat_rate:7
disabled when there is cart items that have the shipping class "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.4WooCommerce:3.1.1
My Site is this: http://www.minimoto.me/WordPress: 4.8.4WooCommerce: 3.1.1
任何帮助将不胜感激.
推荐答案
更新2019:您应该尝试使用这种更短,更紧凑,更有效的方法:
Update 2019: You should try instead this shorter, compact and effective 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 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文件或任何插件文件中.
经过测试,可以正常工作.
Tested and works.
相关:
针对许多不同的送货方式(与您的评论有关)进行更新:
Update for many different shipping methods (related to your comments):
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;
}
经过测试,可以正常工作...
Tested and works…
查找运输类别ID.
1)在wp_terms
表下的数据库中:
1) In the database under wp_terms
table:
搜索术语名称或术语段,您将获得术语ID(运输类别ID).
Search for a term name or a term slug and you will get the term ID (the shipping class ID).
2)在Woocommerce运费设置中,使用浏览器html检查器工具编辑固定费率",检查运费类别费率字段,例如:
2) On Woocommerce shipping settings editing a "Flat rate", with your browser html inspector tool, inspect a shipping Class rate field like:
在归因名称属性中,您具有woocommerce_flat_rate_class_cost_64
.所以64是运输类的ID.
In the imput name attribute you have woocommerce_flat_rate_class_cost_64
. So 64 is the ID for the shipping class.
获取送货方式费率ID:
这篇关于隐藏WooCommerce中特定运输类别的运输方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!