<?php /* * 优惠分摊,算法很多,这里是从shopnc挖出来,适合优惠条件过滤的算法,实质很简单,但是理解难度还是有一点 * * 一个订单的商品,如果不参与某种活动,需要分摊优惠,一般来说需要分摊的有购物金,积分,优惠券,满减等 ,是在整个订单的上来使用优惠 * 多件N折,限制抢购,团购等都是在商品单价上面做的 * 所以难点就在于如何去分摊优惠比如,购物金,积分,订单取消之后需要返还,商品价格-购物金-积分之后的钱,才是需要退给用户的钱 * * * useuserpoint 拆购物金分摊 * usediscount 拆代金卷分摊 * use_offer 除去购物金和折价券之外的优惠分摊,目前来说就是满减的分摊 * * 按照不同活动类型来计算此次参与活动商品总金额,再按参与商品金额goods_total的占比来分配分摊的优惠,$type就是来对应相关的优惠分摊类型 */ function _splitPrice(&$goods_info, $tmprice, $type) { $goods_num = count($goods_info); $total = 0; foreach($goods_info as $k => $v) { if($type=='use_offer'){ if($v['is_own_shop'] == 1) { if($goods_num>1){ if(!$v['yihe_supplier_deliver']){ //第三方发货的不要摊到满减等优惠 if(!$v['more_goods_discount_info']){ //多件N折商品跳过满减优惠分摊 $total += $v['goods_total']; } } }else{ $total += $v['goods_total']; } } }else{ if($v['is_own_shop'] == 1) { $total += $v['goods_total']; } } } $tmp = $tmprice; //表面看很不理解,但是其他2个$type对应的值写出来就很明白就是一个根据不同优惠分摊类型经行计算总价格的, //下面的也是不过是计算分摊具体优惠转换成存打数据库的字段 // foreach($goods_info as $k => $v) { // if($type=='use_offer'){ // if($v['is_own_shop'] == 1) { // if($goods_num>1){ // if(!$v['yihe_supplier_deliver']){ //第三方发货的不要摊到满减等优惠 // if(!$v['more_goods_discount_info']){ //多件N折商品跳过满减优惠分摊 // $total += $v['goods_total']; // } // } // }else{ // $total += $v['goods_total']; // } // } // }elseif($type=='usediscount'){ // if($v['is_own_shop'] == 1) { // $total += $v['goods_total']; // } // }elseif($type=='useuserpoint'){ // if($v['is_own_shop'] == 1) { // $total += $v['goods_total']; // } // // } // } foreach($goods_info as $k => $v) { //第三方发货的跳过 //多件N折商品跳过满减优惠分摊 if($type == 'use_offer'&&$goods_num>1&&$v['yihe_supplier_deliver']){ continue; } if($type == 'use_offer'&&$goods_num>1&&$v['more_goods_discount_info']){ continue; } if($v['is_own_shop'] == 1) { $splitVal = intval($v['goods_total']/$total*$tmprice) + 1; if($splitVal > $tmp) { $splitVal = $tmp; } $tmp -= $splitVal; if($type == 'useuserpoint') { $goods_info[$k]['goods_splituserpoint'] = $splitVal; } elseif($type == 'usediscount') { $goods_info[$k]['goods_splitusediscount'] = $splitVal; }elseif($type=='use_offer'){ $goods_info[$k]['goods_split_use_offer'] = $splitVal; } if($tmp == 0) { break; } } } // P($goods_info); // die; }