wordpress - Woocommerce 数量范围运费
问题描述
我需要像这样设置运费:
1 - 5 个,60 美元 6 - 10 个,90 美元
我怎样才能做到这一点?因此,如果有人从产品中购买 3 件,则收取 60 件运费,如果 6 件或更多件,则收取 90 件运费。
woocommerce 提供的基本 [qty] 占位符无法做到这一点,也没有找到任何可以做到这一点的插件。
解决方案
也许是这样的?在您的活动主题 functions.php 文件中添加以下功能
add_filter( 'woocommerce_package_rates', 'custom_shipping_costs', 20, 2 );
function custom_shipping_costs( $rates, $package ) {
global $woocommerce;
$qty = $woocommerce->cart->cart_contents_count;
//error_log($qty);
foreach( $rates as $rate_key => $rate ){
// Excluding free shipping methods
if( $rate->method_id != 'free_shipping'){
// Between 1 and 5
if($qty > 0 && $qty <= 5):
$rates[$rate_key]->cost = '60';
// Between 6 and 10
elseif($qty > 5 && $qty <= 10):
$rates[$rate_key]->cost = '90';
else:
// For over 10
$rates[$rate_key]->cost = '60';
endif;
}
}
return $rates;
}
推荐阅读
- haskell - Haskell向构造函数添加约束
- javascript - 如何检测 document.getSelection().modify('extend', 'forward', 'word') 是否到达文档末尾?
- python-3.x - 火币工作python 3.6示例创建url(含签名)
- python - 如何在 Pandas 中进行 VLOOKUP?
- python - 当 y 坐标匹配或 z 坐标时绘制线的程序
- azure - 有没有办法在 powerBI 交互式的天蓝色地图中制作参考层?
- android - 如何在 Android 中将 TabLayout 中的自定义选项卡项与 ViewPager2 一起使用,TabLayoutMediator 不显示自定义 TabItem
- python - 创建一个矩阵,每行和每列最多包含一个值
- ios - AppStoreConnect 上以 Invalid Binary 为原因的应用被拒绝
- ios - 当用户开始在 Swift 中移动路线时动态更改 MapView 折线样式