使用 Hide shipping method for specific shipping classes in woocommerce答案代码,我试图隐藏基于航运类别的航运选项。
大件元素和小件元素在我们的网站上都有自己的运输类别,我们只想为小件元素提供 express 服务。
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,我只能找到 slug 和运输类名称
// 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 文件...
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.
To find the shipping methods IDs and the shipping classes IDs see below…
查找装运类别 ID。
1) 在wp_terms
表下的数据库中:
搜索术语名称或术语 slug,您将获得术语 ID(发货类别 ID)。
2) 在 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 attributename
like:
关于php - 如何在 WooCommerce 中找到运输类别 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53769697/