問題描述
我一直在搜索代碼以過濾除本地提貨以外的任何送貨方式.
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).
我只發(fā)現過時的代碼并且在 WC3+ 上不起作用.
I only found code that was outdated and does not work on 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;
}
代碼位于活動子主題(或活動主題)的 function.php 文件中.經測試有效
Code goes in function.php file of your active child theme (or active theme). Tested and works
然后在 StackOverFlow 使用 woocommerce_package_rates
搜索最近的答案 將允許您完成代碼.
Then in StackOverFlow searching for recent answer with
woocommerce_package_rates
will allow you to finish your code.
這篇關于根據 Woocommerce 3 中的運輸類別過濾運輸方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!