Wordpress Shipping class公式帮助:如果数量为1-3,是否收取X金额?

Wordpress Shipping class公式帮助:如果数量为1-3,是否收取X金额?,wordpress,woocommerce,Wordpress,Woocommerce,我在店里卖印刷品。我把它们打印出来,装在试管里运出去。每根试管最多可容纳三张照片,发送一支试管的成本为X数量 我怎样才能写: 如果数量为1-3,则收取X… 如果数量为4-6,则收费2倍… 如果数量为7-9,则收费3倍 等等,等等 或者有更好的方法吗?您可以使用 woocommerce\u套餐价格 胡克操纵运输成本 function woocommerce_package_rates( $rates ) { //Assuming charge X you mentioned is 5 $cust

我在店里卖印刷品。我把它们打印出来,装在试管里运出去。每根试管最多可容纳三张照片,发送一支试管的成本为X数量

我怎样才能写:

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

等等,等等

或者有更好的方法吗?

您可以使用

woocommerce\u套餐价格

胡克操纵运输成本

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' );
function-woodcommerce\u-package\u费率($rates){
//假设你提到的费用X是5
$custom_shipping_成本=5;
$total_items=WC()->cart->get_cart_contents_count();
如果($total_items>3&&$total_items<7)$custom_shipping_cost*=2;
如果($total_items>6&$total_items<10)$custom_shipping_cost*=3;
如果($total_items>9&$total_items<13)$custom_shipping_cost*=4;
//您可以在此处添加更多其他if语句
//或者试试我认为更聪明的方法:
/*
*$乘数=下限($total_items/4)+1;
*$custom_shipping_cost*=$multiplier;
*/
foreach($key=>$rate)
{
$rates[$key]->cost=$custom\u shipping\u cost;
}
返回美元汇率;
}
添加_过滤器(“woocommerce_套餐_费率”、“woocommerce_套餐_费率”);
希望这有帮助。代码转到主题或子主题的functions.php(哪个更好)