[英]Conditionally Hide WooCommerce Shipping methods based on shipping class
Using WooCommerce v3.2.4 on This site (here) (WP v4.9) and 11 products with the shipping class of Overweight/Oversize that have a flat rate applied to them: 在本网站(此处) (WP v4.9)上使用WooCommerce v3.2.4和11种产品,其运输类别为Overweight / Oversize,适用于固定费率:
$20 to Canada and $25 to the US. 加拿大20 美元 ,美国25美元。
All other products have flat rate shipping of $10 (Canada) and $15 (US), unless the order is over $100 , then free shipping is applied automatically. 所有其他产品的统一运费为10美元 (加拿大)和15美元 (美国),除非订单超过100美元 ,然后自动运送免费送货。
My client wants free shipping to be disabled if there are any overweight/oversize items in the cart. 如果购物车中有超重/超大商品,我的客户希望免费送货。 The problem is that the cart says there are no shipping methods available when there are a mix of regular and oversize items in the cart, and no shipping methods are applied.
问题是购物车说当购物车中有常规和超大尺寸物品混合时没有可用的运输方法,并且没有运输方法。
I'm using XAdapter Woocommerce Shipping Table Rate plugin to apply the higher cost to the "Overweight" Shipping Classes. 我正在使用XAdapter Woocommerce Shipping Table Rate插件将更高的成本应用于“超重”运输类。
UPDATE UPDATE
I deactivated this plugin, as I realized I could just use the WooCommerce Shipping Zone settings to set a flat rate for specific shipping classes. 我停用了此插件,因为我意识到我可以使用WooCommerce Shipping Zone设置为特定的运输类别设置固定费率。 See screenshot below:
见下面的截图:
I am using some code to: 我正在使用一些代码:
Here is that code: 这是代码:
add_filter('woocommerce_package_rates', 'wf_hide_shipping_method_based_on_shipping_class', 100, 2);
function wf_hide_shipping_method_based_on_shipping_class($available_shipping_methods, $package){
$hide_when_shipping_class_exist = array(
163 => array(
'flat_rate:1',
'flat_rate:2',
'free_shipping:3',
'free_shipping:5'
)
);
$hide_when_shipping_class_not_exist = array(
163 => array( 'wf_woocommerce_shipping_pro:overweightoversizeoverweight')
);
$shipping_class_in_cart = array();
foreach(WC()->cart->cart_contents as $key => $values) {
$shipping_class_in_cart[] = $values['data']->get_shipping_class_id();
}
foreach($hide_when_shipping_class_exist as $class_id => $methods) {
if(in_array($class_id, $shipping_class_in_cart)){
foreach($methods as & $current_method) {
unset($available_shipping_methods[$current_method]);
}
}
}
foreach($hide_when_shipping_class_not_exist as $class_id => $methods) {
if(!in_array($class_id, $shipping_class_in_cart)){
foreach($methods as & $current_method) {
unset($available_shipping_methods[$current_method]);
}
}
}
return $available_shipping_methods;
}
EDIT 编辑
Here is a list of IDs of the rates per shipping zone: 以下是每个送货区的费率ID列表:
Canada 加拿大
flat_rate:1
flat_rate:1
free_shipping:3
free_shipping:3
local_pickup:4
local_pickup:4
USA 美国
flat_rate:2
flat_rate:2
free_shipping:5
free_shipping:5
UPDATE 2: (Without any plugin need, just settings and code) 更新2 :( 没有任何插件需要,只需设置和代码)
The function below will always show "Local pickup" shipping for canada and will: 以下功能将始终显示加拿大的“本地取件”运费,并将:
Here is that code: 这是代码:
add_filter('woocommerce_package_rates', 'wf_hide_shipping_method_based_on_shipping_class', 100, 2);
function wf_hide_shipping_method_based_on_shipping_class($rates, $package){
// Defining & Initializing variables
$free_shipping_rates = array(
'free_shipping:3',
'free_shipping:5'
);
// Defining & Initializing variables
$shipping_class_id = 163;
$free = array();
$over_found = $has_free = false;
// Check if "Oversize" shipping class (163) is in cart items
foreach(WC()->cart->get_cart() as $key => $cart_item){
if($cart_item['data']->get_shipping_class_id() == $shipping_class_id ){
$over_found = true;
break;
}
}
// 1. Hiding free shipping but always show Local pickup for Canada
if( $over_found ){
foreach($free_shipping_rates as $rate_id) {
unset( $rates[$rate_id] );
}
}
// 2. Hiding Flat rate OR Free shipping --> depending on cart amount
// (but always show Local pickup for Canada)
else {
foreach ( $rates as $rate_id => $rate ) {
// Hide all "Flat rates" when "Free Shipping" is available
if ( 'free_shipping' === $rate->method_id ) {
$free[ $rate_id ] = $rate;
$has_free = true;
} elseif ( 'local_pickup' === $rate->method_id ) {
$free[ $rate_id ] = $rate;
}
}
return $has_free ? $free : $rates;
}
return $rates;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file. 代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。
Tested on WooCommerce 3 and works. 在WooCommerce 3上测试并且有效。
Refresh the shipping caches (needed sometimes) :
刷新运输缓存(有时需要) :
1) First empty your cart.1)首先清空你的购物车。
2) This code is already saved on your function.php file.2)此代码已保存在function.php文件中。
3) Go in a shipping zone settings and disable one "flat rate" (for example) and "save".3)进入送货区设置并禁用一个“统一费率” (例如)和“保存”。 Then re-enable that "flat rate" and "save".
然后重新启用“统一费率”和“保存”。 You are done and you can test it.
你完成了,你可以测试它。
This is possible by using Woocommerce Shipping Table Rate , You just need to add the extra rules. 这可以通过使用Woocommerce运输表费率 ,您只需要添加额外的规则。 You need to add 7 shipping rules, You can see the shipping rules in the below image: Image contains the shipping rule you need to add
您需要添加7个送货规则,您可以在下图中看到送货规则: 图片包含您需要添加的送货规则
For the above rules, you will get the desired results at a cart page. 对于上述规则,您将在购物车页面上获得所需的结果。 I have attached some screenshot of the cart page in different cases for your references: Note: Shipping class of Hat: Regular & Hoodie: Over weight.
我在不同的情况下附上了一些购物车页面的截图供您参考:注:帽子的运输类:普通和连帽衫:超重。
Regular and overweight product ordered together 定期和超重的产品一起订购
Over wight product with orders over $100 订单超过100美元的超级产品
Free shipping for orders amount over $100 with regular items 订单金额超过100美元的常规商品免运费
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.