在 WooCommerce 中隐藏特定运输 类 的所有固定费率运输方式
Hide all Flat rate Shipping methods for specific shipping classes in WooCommerce
我正在建立一个带送货的网站,但我有针对所有送货区收集的物品和可以在所有送货区发送的物品。
所以我已经为所有区域设置了运送 类。
我正在使用 "" 答案代码,这正是我所需要的。
但是 不是输入每个 flat_rate id 有没有一种方法可以定位 所有统一费率送货方式 ,所以当我添加其他统一运费设置时,它会起作用,而无需我更改代码。
希望你明白我的意思。感谢任何帮助。
要将其用于所有 "flat rate" 送货方式 和一些其他已定义的送货方式,您将改用以下方式:
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 (others than "flat rate")
$method_key_ids = array('local_pickup:3');
$found = false;
// Checking in cart items
foreach( $package['contents'] as $cart_item ){
// If we find the shipping class
if( $cart_item['data']->get_shipping_class_id() == $class ){
$found = true;
break; // Stop the loop
}
}
if( ! $found )
return $rates;
// Loop through shipping methods
foreach( $rates as $rate_key => $rate ) {
// Targetting "Flat rate" and other defined shipping mehods
if( 'flat_rate' === $rate->method_id || in_array($rate->id, $method_key_ids) ) {
unset($rates[$rate_key]);
}
}
return $rates;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
Refresh the shipping caches: (required)
- This code is already saved on your active theme's function.php file.
- The cart is empty
- In a shipping zone settings, disable / save any shipping method, then enable back / save.
我正在建立一个带送货的网站,但我有针对所有送货区收集的物品和可以在所有送货区发送的物品。
所以我已经为所有区域设置了运送 类。
我正在使用 "
但是 不是输入每个 flat_rate id 有没有一种方法可以定位 所有统一费率送货方式 ,所以当我添加其他统一运费设置时,它会起作用,而无需我更改代码。
希望你明白我的意思。感谢任何帮助。
要将其用于所有 "flat rate" 送货方式 和一些其他已定义的送货方式,您将改用以下方式:
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 (others than "flat rate")
$method_key_ids = array('local_pickup:3');
$found = false;
// Checking in cart items
foreach( $package['contents'] as $cart_item ){
// If we find the shipping class
if( $cart_item['data']->get_shipping_class_id() == $class ){
$found = true;
break; // Stop the loop
}
}
if( ! $found )
return $rates;
// Loop through shipping methods
foreach( $rates as $rate_key => $rate ) {
// Targetting "Flat rate" and other defined shipping mehods
if( 'flat_rate' === $rate->method_id || in_array($rate->id, $method_key_ids) ) {
unset($rates[$rate_key]);
}
}
return $rates;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
Refresh the shipping caches: (required)
- This code is already saved on your active theme's function.php file.
- The cart is empty
- In a shipping zone settings, disable / save any shipping method, then enable back / save.