如何解决如果购物车中有超过4种特定运输类别的商品,请隐藏一些运输方式 编辑-添加:潜在的解决方案-添加:
这是场景:
我使用UPS运送饮料托盘。但是,将5盘饮料装入包装盒变得非常困难。因此,我想禁用UPS的送货方式,仅在客户订购5盘或更多盘饮料时才显示统一费率送货。我大约有7种饮料,但是我可以将这些饮料添加到运输类中以简化代码。
我想扩展此代码,以包括特定类别中的产品数量,或者可能包括运输类别中的产品出现的次数。因此,如果购物车在此特定的送货类别中有5种或以上的产品,则应删除我在array下指定的送货方式。
如何扩展此代码以包含产品数量?
//如果我们发现运输类别和该运输类别中的产品数量等于或大于5。
add_filter( 'woocommerce_package_rates','hide_shipping_method_based_on_shipping_class',10,2 );
function hide_shipping_method_based_on_shipping_class( $rates,$package )
{
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Shipping Class To Find
$class = 182;
// Number Of Shipping Class Items In Cart
$amount = 5;
// Shipping Methods To Hide
$method_key_ids = array('wf_shipping_ups:07','wf_shipping_ups:08','wf_shipping_ups:11','wf_shipping_ups:54','wf_shipping_ups:65','wf_shipping_ups:70','wf_shipping_ups:74','free_shipping:2','request_shipping_quote');
// Checking In Cart Items
foreach( $package['contents'] as $item ) {
// If We Find The Shipping Class and Number of Items
if( $item['data']->get_shipping_class_id() == $class && count($package['contents']) >= $amount ){
foreach( $method_key_ids as $method_key_id ){
unset($rates[$method_key_id]); // Remove Targeted Methods
}
break; // Stop The Loop
}
}
return $rates;
}
编辑-添加:
自从我从PluginHive购买了“带有打印标签的WooCommerce UPS运输插件”后,我就可以使用其“管理运输方法”插件,该插件可以执行以下操作:
设置多个规则以从各种运输类别中排除各种运输方法。 在第一次出现时中断顺序。
我设置的规则如下:
- 对于150类(首次出现故障)-未设置:
wf_shipping_ups:07, wf_shipping_ups:08, wf_shipping_ups:11, wf_shipping_ups:54, wf_shipping_ups:65, wf_shipping_ups:70, wf_shipping_ups:74, free_shipping:2, request_shipping_quote。
- 对于151类-未设置: flat_rate:20, flat_rate:21。
在上面的代码中,我已经为要定位的产品创建了第三类182。仅当购物车中添加了少于5类的物品时,才应将其视为151类。
但是,如果购物车中添加了5个或更多物品,则应将其视为150类。
那是我的困境。
潜在的解决方案-添加:
我想出了解决问题的方法。如果购物车中的产品数量为5个或更多,则@LoicTheAztec代码可帮助我取消给定运输类别的运输方式。
我现在要做的是针对相同的运输类别(182)取消设置其他两种运输方法(flat_rate:20和flat_rate:21),这会引起冲突,但是这次是4车中的产品数量或更少(=
然后我可以使用现有的插件来创建以下规则:
第一次出现故障(检查)
- 对于150类-未设置:
wf_shipping_ups:07, wf_shipping_ups:08, wf_shipping_ups:11, wf_shipping_ups:54, wf_shipping_ups:65, wf_shipping_ups:70, wf_shipping_ups:74, free_shipping:2, request_shipping_quote。
- 对于182类-未设置:
一无所有-因为两种代码都会创建逻辑
- 对于151类-未设置:
flat_rate:20, flat_rate:21。
这应该解决由插件引起的冲突。
百万美元的问题是...我能以某种方式使用@LoicTheAztec的解决方案来设置某种最小数量吗?
解决方法
如果来自特定运输类别的总项目为5个或更多,则以下内容将隐藏特定的已定义运输方式:
add_filter( 'woocommerce_package_rates','hide_shipping_method_based_on_shipping_class',10,2 );
function hide_shipping_method_based_on_shipping_class( $rates,$package ) {
$targeted_class_ids = array(182); // Shipping Class To Find
$allowed_max_qty = 4; // Max allowed quantity for the shipping class
$shipping_rates_ids = array( // Shipping Method rates Ids To Hide
'wf_shipping_ups:07','wf_shipping_ups:08','wf_shipping_ups:11','wf_shipping_ups:54','wf_shipping_ups:65','wf_shipping_ups:70','wf_shipping_ups:74','free_shipping:2','request_shipping_quote'
);
$related_total_qty = 0;
// Checking cart items for current package
foreach( $package['contents'] as $key => $cart_item ) {
if( in_array( $cart_item['data']->get_shipping_class_id(),$targeted_class_ids ) ){
$related_total_qty += $cart_item['quantity'];
}
}
// When total allowed quantity is more than allowed (for items from defined shipping classes)
if ( $related_total_qty > $allowed_max_qty ) {
// Hide related defined shipping methods
foreach( $shipping_rates_ids as $shipping_rate_id ) {
if( isset($rates[$shipping_rate_id]) ) {
unset($rates[$shipping_rate_id]); // Remove Targeted Methods
}
}
}
return $rates;
}
代码进入活动子主题(或活动主题)的functions.php文件中。未经测试,它应该可以工作。
刷新发货缓存:
- 此代码已保存在您的functions.php文件中。
- 在运输区域设置中,禁用/保存任何运输方式,然后启用回退/保存。
您已完成,您可以对其进行测试。
处理项目数而不是项目累积数量:
替换:
$related_total_qty += $cart_item['quantity'];
作者
$related_total_qty++;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。