如果取消一个(现金+满减+积分)订单的商品,你如何分摊优惠

优惠分摊,算法很多,这里是从shopnc挖出来,适合优惠条件过滤的算法,实质很简单,但是理解难度还是有一点

 

 

应用场景:

一个订单的商品,如果不参与某种活动的时候,那就需要分摊优惠,一般来说需要分摊的有购物金,积分,优惠券,满减等。而且是在整个订单上来使用优惠。

多件N折,限制抢购,团购等都是在商品单价上面体现的,所以难点就在于如何去分摊优惠,比如一个订单用购物金,积分下单了。订单取消后需要返还。这个时候需要退给用户的钱就是商品的价格-购物金-积分之后的钱了

 

示例截图

如果取消一个(现金+满减+积分)订单的商品,你如何分摊优惠_第1张图片

 

以下提供优惠分摊的方法,有需要的可以借鉴一下

 $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;
    }

 

你可能感兴趣的:(PHP,现金+满减+积分)