因为需求方面要求多买多减,比如买300减100 600减200这样,所以在数据库设计字段的时候,达到优惠的条件full_price设计成了varchar类型,减价的cut_price也设计成了varchar类型。
然后在输入的时候用英文逗号,隔开,这样虽然实现了买不同价格优惠不同的力度,但是弊端就是首先必须要从小到大写上去,因为后台的判断条件是拿着当前商品的总价格,跟这些价格按照索引一个一个比较,当比哪优惠价格小的时候,就判断该商品只满足前一个的优惠价格。
第一种是全站设置一定的满减优惠和需要付邮费的条件和邮费价格。
第二种是每件商品都设置一定的一定的满减优惠和需要付邮费的条件和邮费价格
这里不需要交邮费的条件是购买几件商品就包邮。
前端ajax的代码,将每个商品的价格 商品的数量 传给后台
后台接收数据并回调数据给前台页面。
以下是第一种情况:
第一种情况的不需要邮费的条件是商品的总数量达到一定的数量就可以不用邮费
public function payPrice(){
//找到当前域名的uid
$requet = Request::instance();
$yuming = $requet->domain();
$condition = [
'web_url' => $yuming,
];
$uid = Db::name('auth_group')->where($condition)->field('uid,type')->find();
$uid = $uid['uid'];
//获取前端传递来的值
$data = input('');
$goodsPrice = $data['goodsListPrices']; //每个商品的价格
$moneyType = $data['goodsMoneyType']; //商品的货币类型
$goodsListNumber = $data['goodsListNumber']; //购物车里的商品总数
$goodsPrice = explode(',',$goodsPrice);
$goodsPriceCount = 0; //商品总价
for($i = 0; $i < count($goodsPrice); $i++){
$goodsPriceCount = $goodsPriceCount + $goodsPrice[$i];
}
$config = Db::name('config')->where('uid='.$uid)->field('full_price,cut_price,floor_number,express_price')->find();
$cutPriceCounts = $config['cut_price']; //优惠的价格
$cutPriceCounts = explode(',',$cutPriceCounts); //减的最终价格
$cutPriceCount = 0;
$floorNumber = $config['floor_number']; //满足最低不需要邮费的商品个数
$fullPrices = $config['full_price']; //满减的满足价格区间
$fullPrices = explode(',',$fullPrices);
$fullPrice = 0; //满减的最终条件
$expressPriceCount = $config['express_price']; //运费
$payPrice = 0; //最后需要付费的价格
$isNeedExpress = 0; //是否需要付运费 0不需要 1需要
/*
* 判断商品能达到何种优惠
* */
for($i = 0; $i < count($fullPrices); $i++){
if($fullPrices[$i] < $goodsPriceCount ){
$fullPrice = $fullPrices[$i];
$cutPriceCount = $cutPriceCounts[$i];
}
}
//如果商品总价大于满减的优惠价格 且 商品数目大于需要付邮费的商品数目
if($goodsPriceCount >= $fullPrice && $goodsListNumber >= $floorNumber){
$expressPriceCount = 0;
$payPrice = $goodsPriceCount - $cutPriceCount;
}
//如果商品总价大于满减的优惠价格 且 商品数目小于需要付邮费的商品数目
else if($goodsPriceCount >= $fullPrice && $goodsListNumber < $floorNumber){
$expressPriceCount = $expressPriceCount;
$payPrice = $goodsPriceCount - $cutPriceCount + $expressPriceCount;
$isNeedExpress = 1; //是否需要付运费
}
//如果商品总价小于满减的优惠价格 且 商品数目大于需要付邮费的商品数目
else if($goodsPriceCount < $fullPrice && $goodsListNumber >= $floorNumber){
$expressPriceCount = 0;
$cutPriceCount = 0;
$payPrice = $goodsPriceCount;
$isNeedExpress = 0; //是否需要付运费
}
//如果商品总价小于满减的优惠价格 且 商品数目小于需要付邮费的商品数目
else if($goodsPriceCount < $fullPrice && $goodsListNumber < $floorNumber){
$expressPriceCount = $expressPriceCount;
$cutPriceCount = 0;
$payPrice = $goodsPriceCount + $expressPriceCount;
$isNeedExpress = 1; //是否需要付运费
}
$msg = [
'cutPriceCount' => $cutPriceCount, //满减的优惠价格
'expressPriceCount' => $expressPriceCount, //邮费
'payPrice' => $payPrice, //最终价格
'floorNumber' => $floorNumber, //满足最低邮费的商品个数
'isNeedExpress' => $isNeedExpress, //是否需要付邮费 0不需要 1需要
'moneyType' => $moneyType //货币类型
];
return $msg;
}
-----------------
第二种的情况如下:
第二种情况的需求是 商品达到一定的价格就不需要邮费
第二种情况需要ajax传给后台商品的id,具体思路就是先把每个订单里的商品,计算出他们对应的满减优惠,然后加起来得到最终的优惠总价。然后找出订单里每个商品不需要交邮费的价格做比较,得到最大价格与商品总价做比较,看看是否满足不需要邮费。
public function payPrice(){
$data = input('');
$goodsId = $data['gooodsId'];
$goodsPrice = $data['goodsListPrices'];
$goodsId = explode(',',$goodsId);
$goodsPrice = explode(',',$goodsPrice);
$goodsList = [];
for($i = 0; $i < count($goodsId); $i++){
$goodsList[$i] = Db::name('goods_goods')->where('id='.$goodsId[$i])->field('full_price,cut_price,floor_price,express_price,id,goods_moneytype')->find();
$goodsList[$i]['price'] = $goodsPrice[$i];
}
$moneyType = $goodsList[0]['goods_moneytype'];
$cutPriceCount = 0; //优惠的价格
$floorPrice = []; //购物车页面里所有商品,选出需要支付运费的最大价格
$expressPriceCount = []; //运费
$payPrice = 0; //最后需要付款的价格
$isNeedExpress = 0; //是否需要付运费
foreach($goodsList as $key=>$value){
$floorPrice[$key] = $goodsList[$key]['floor_price'];
$expressPriceCount[$key] = $goodsList[$key]['express_price'];
$payPrice = $payPrice + $value['price']; //商品总原价
/*
* 如果购物车里的当前商品的价格高于优惠价格 进行优惠满减
* */
if($value['price'] > $value['full_price']){
$value['price'] = $value['price'] - $value['cut_price'];
$cutPriceCount = $cutPriceCount + $value['cut_price']; //减价
}
}
$floorPrice = max($floorPrice); //所选的套餐最大的所需付运费满足的价格条件
$expressPriceCount = max($expressPriceCount); //所选商品最大的运费价格
//如果商品的总原价大于等于所选商品中最大不需要付邮费的价格
if($payPrice >= $floorPrice){
$expressPriceCount = 0;
$payPrice = $payPrice - $cutPriceCount;
}
//如果商品的总原价小于所选商品中最大需要付邮费的价格
else if($payPrice < $floorPrice){
$expressPriceCount = $expressPriceCount;
$payPrice = $payPrice - $cutPriceCount;
$isNeedExpress = 1; //是否需要付运费
}
$msg = [
'cutPriceCount' => $cutPriceCount,
'expressPriceCount' => $expressPriceCount,
'payPrice' => $payPrice,
'floorPrice' => $floorPrice,
'isNeedExpress' => $isNeedExpress,
'moneyType' => $moneyType
];
return $msg;
}