使用隐藏运输方法为特定运输类在WooCommerce答案代码,我试图隐藏运输选项基于运输类。
大项目和小项目有自己的航运类在我们的网站上,我们只想提供快递小项目。
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( WC()->cart->get_cart() as $cart_item ){
// If we find the shipping class
if( $cart_item['data']->get_shipping_class_id() == $class ){
unset($rates[$method_key_id]); // Remove the targeted method
break; // Stop the loop
}
}
return $rates;
}
我知道如何实现代码,但是我不知道在哪里可以找到运输类id,我只能找到段塞和运输类名
// HERE define your shipping class to find
$class = 92;
在上面的示例中,在哪里可以找到替换92的Id?
提前谢谢。
将
要使它与运输类Slugs一起工作,您将使用WC_Product
get_shipping_class()
方法,而不是这样:
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 SLUG
$class_slug = 'large';
// HERE define the shipping method to hide
$method_key_id = 'flat_rate:7';
// Checking in cart items
foreach( WC()->cart->get_cart() as $cart_item ){
// If we find the shipping class
if( $cart_item['data']->get_shipping_class() == $class_slug ){
unset($rates[$method_key_id]); // Remove the targeted method
break; // Stop the loop
}
}
return $rates;
}
代码function.php活动子主题(或活动主题)的文件中...
有时,您可能需要刷新前往送货区的送货方式,然后禁用/保存和重新启用/保存您的“统一费率”送货方式。
要查找装运方法ID和装运类ID,请参见下面…
查找装运类ID。
1) 在数据库中的wp_terms
表中:
搜索一个术语名称或术语slug,您将获得术语ID(装运类ID)。
2) 在Woocommerce配送设置编辑“统一费率”时,使用浏览器html inspector工具检查配送类费率字段,如:
在输入名称属性中有woocommerce\u flat\u rate\u class\u cost\u 64
。所以64是shipping类的ID。
获取发货方式费率ID:
要获取相关的运输方法费率ID,如扁平费率:12
,请使用浏览器代码检查器检查每个相关单选按钮属性名称
,如: