[英]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 cart (among other products).我一直在搜索代码来过滤除本地提货以外的任何运输方式,在结账时,当购物车中(以及其他产品)选择了特定运输类别(仅提货,例如)的产品时。
I only found code that was outdated and does not work on WC3+.我只发现过时的代码并且在 WC3+ 上不起作用。
Here is the way to filter out any shipping methods other than local pick up, when a product that has a specific shipping class enabled:当启用特定运输类别的产品时,以下是过滤除本地提货以外的任何运输方式的方法:
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;
}
Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and works
测试和工作
Then in StackOverFlow searching for recent answer with
woocommerce_package_rates
will allow you to finish your code.然后在 StackOverFlow 中使用
woocommerce_package_rates
搜索最近的答案将允许您完成代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.