我在我的店里卖印刷品。我把它们打印出来,然后装在管子里运出去。每个试管最多可以容纳三张照片,发送一张试管的费用是X。
我该如何写:
如果数量为1-3,则收费X…如果数量为4-6,则收取2倍的…如果数量为7-9,则收取3倍的…
等等?
还是有更好的方法?
发布于 2019-10-31 20:16:56
您可以使用
woocommerce_package_rates
操纵运费的钩子。
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 (哪个更好)。
https://stackoverflow.com/questions/58638396
复制相似问题