我一直在搜索代码,以便在结帐时筛选出除本地提货以外的任何运输方式,当选择了特定运输类别的产品(仅限皮卡,例如)在购物车中(以及其他产品)
我只找到过时且无法在WC3 +上运行的代码。
答案 0 :(得分:1)
以下是在启用了特定运费等级的产品时过滤掉本地提货以外的任何运输方式的方法:
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文件中。经过测试和工作
然后在StackOverFlow中searching for recent answer with
woocommerce_package_rates
将允许您完成代码。