根据 Woocommerce 3 中的运费 class 过滤送货方式
Filter Shipping method based on shipping class in Woocommerce 3
I have been searching for code to filter out any shipping methods other than local pick up, on checkout, when a product that has a specific shipping class selected (Only pickup, ex.) is in the购物车(以及其他产品)。
我只发现过时的代码,不适用于 WC3+。
当产品启用了特定运输方式 class 时,这里是过滤除当地取货以外的任何运输方式的方法:
add_filter( 'woocommerce_package_rates', 'custom_shipping_rates', 100, 2 );
function custom_shipping_rates( $rates, $package ) {
$shipping_class = 64; // HERE set the shipping class ID
$found = false;
// Loop through cart items Checking for the defined shipping method
foreach( $package['contents'] as $cart_item ) {
if ( $cart_item['data']->get_shipping_class_id() == $shipping_class ){
$found = true;
break;
}
}
if ( ! $found ) return $rates; // If not found we exit
// Loop through shipping methods
foreach( $rates as $rate_key => $rate ) {
// all other shipping methods other than "Local Pickup"
if ( 'local_pickup' !== $rate->method_id && $found ){
// Your code here
}
}
return $rates;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效
Then in Whosebug searching for recent answer with woocommerce_package_rates
will allow you to finish your code.
I have been searching for code to filter out any shipping methods other than local pick up, on checkout, when a product that has a specific shipping class selected (Only pickup, ex.) is in the购物车(以及其他产品)。
我只发现过时的代码,不适用于 WC3+。
当产品启用了特定运输方式 class 时,这里是过滤除当地取货以外的任何运输方式的方法:
add_filter( 'woocommerce_package_rates', 'custom_shipping_rates', 100, 2 );
function custom_shipping_rates( $rates, $package ) {
$shipping_class = 64; // HERE set the shipping class ID
$found = false;
// Loop through cart items Checking for the defined shipping method
foreach( $package['contents'] as $cart_item ) {
if ( $cart_item['data']->get_shipping_class_id() == $shipping_class ){
$found = true;
break;
}
}
if ( ! $found ) return $rates; // If not found we exit
// Loop through shipping methods
foreach( $rates as $rate_key => $rate ) {
// all other shipping methods other than "Local Pickup"
if ( 'local_pickup' !== $rate->method_id && $found ){
// Your code here
}
}
return $rates;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效
Then in Whosebug searching for recent answer with
woocommerce_package_rates
will allow you to finish your code.