//type=1是按照件数
if($template['type'] == 1){
// echo '按数量计算,商品数量'.$number.'<br>';
// echo $items['first'].'件内'.$items['amount'].'元,每多'.$items['add'].'件,增加'.$items['add_amount'].'元<br>';
if($number <= $items['first']){
// echo '小于首数量<br>';
$amount = $items['amount'];
} else {
// echo '首件费用:'.$items['amount'].';续费:'.$items['add_amount'].'续件:'.$step.';<br>';
$step = ceil(($number - $items['first'])/$items['add']);
$amount = $items['amount']+$step*$items['add_amount'];
}
} else {
// echo '按重量计算,商品重量:'.$weight.'<br>';
//按重量
if($weight <= $items['first']){
// echo '小于等于首重<br>';
$amount = $items['amount'];
} else {
// echo $items['first'].'g内'.$items['amount'].'元,每多'.$items['add'].'g,增加'.$items['add_amount'].'元<br>';
// echo '重量:'.$weight.'<br>';
$step = ceil(($weight - $items['first'])/$items['add']);
// echo '续重:'.$step.'<br>';
$amount = $items['amount']+$step*$items['add_amount'];
}
}
//这就是结果了加油
$amount;
等等,我封装以下哈
//$freightitems=["first"=>"首件或者首重","amount"=>"首件或者首重的费用","add"=>"续件或者续重","add_amount"=>"续件或者续重的费用","type"=>"1=按照件数"];
$freightitems=["first"=>"1","amount"=>"1","add"=>"1","add_amount"=>"1","type"=>"1"];
function freight($number,$a_weight, $freightitems){
$amount=0;
if($freightitems['type'] == 1){
// echo '按数量计算,商品数量'.$number.'<br>';
// echo $items['first'].'件内'.$items['amount'].'元,每多'.$items['add'].'件,增加'.$items['add_amount'].'元<br>';
if($number <= $freightitems['first']){
// echo '小于首数量<br>';
$amount = $freightitems['amount'];
} else {
// echo '首件费用:'.$items['amount'].';续费:'.$items['add_amount'].'续件:'.$step.';<br>';
$step = ceil(($number - $freightitems['first'])/$freightitems['add']);
$amount = $freightitems['amount']+$step*$freightitems['add_amount'];
}
} else {
// echo '按重量计算,商品重量:'.$weight.'<br>';
//按重量
if($weight <= $freightitems['first']){
// echo '小于等于首重<br>';
$amount = $freightitems['amount'];
} else {
// echo $items['first'].'g内'.$items['amount'].'元,每多'.$items['add'].'g,增加'.$items['add_amount'].'元<br>';
// echo '重量:'.$weight.'<br>';
$step = ceil(($weight - $freightitems['first'])/$freightitems['add']);
// echo '续重:'.$step.'<br>';
$amount = $freightitems['amount']+$step*$freightitems['add_amount'];
}
}
//这就是结果了加油
return $amount;
}