首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >发货分类公式帮助:如果数量为1-3,是否收取X金额?

发货分类公式帮助:如果数量为1-3,是否收取X金额?
EN

Stack Overflow用户
提问于 2019-10-31 15:11:43
回答 1查看 53关注 0票数 0

我在我的店里卖印刷品。我把它们打印出来,然后装在管子里运出去。每个试管最多可以容纳三张照片,发送一张试管的费用是X。

我该如何写:

如果数量为1-3,则收费X…如果数量为4-6,则收取2倍的…如果数量为7-9,则收取3倍的…

等等?

还是有更好的方法?

EN

回答 1

Stack Overflow用户

发布于 2019-10-31 20:16:56

您可以使用

woocommerce_package_rates

操纵运费的钩子。

代码语言:javascript
复制
function woocommerce_package_rates( $rates ) {

//Assuming charge X you mentioned is 5
$custom_shipping_cost = 5;
$total_items = WC()->cart->get_cart_contents_count();

if($total_items > 3 && $total_items < 7) $custom_shipping_cost *= 2;
else if ($total_items > 6 && $total_items < 10) $custom_shipping_cost *= 3;
else if ($total_items > 9 && $total_items < 13) $custom_shipping_cost *= 4;
//you may add more else if statements here
//or try this which is smarter I believe:
/*
* $multiplier = floor( $total_items / 4 ) + 1;
* $custom_shipping_cost *= $multiplier;
*/

foreach($rates as $key => $rate ) 
{
    $rates[$key]->cost = $custom_shipping_cost;
}

return $rates;
}

add_filter( 'woocommerce_package_rates', 'woocommerce_package_rates' );

希望这能有所帮助。代码转到你的主题或子主题的functions.php (哪个更好)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58638396

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档