我该怎么办"买3并支付2"数学?

时间:2017-09-21 18:16:35

标签: php math e-commerce shopping-cart percentage

我有一个购物车,我需要建立优惠功能,我已经完成了百分比关闭,我需要一些数量促销帮助。

例如,如果顾客购买3件商品,他只需支付2件,但如果他想购买5件商品,我必须仅为3件商品申请促销,2件全价购买。

这是我的代码

    $quand_in_cart =  '5'; //quantity for product X in the cart
    $prod_price = '1.5'; //product price

    $percentage_off = NULL;
    $buy_any = 3; //promotion on 3 items
    $for_the_price = 2; //pay only 2

    if($percentage_off and $quand_in_cart >= $buy_any){

        $price_discount = ($percentage_off/100) * $quand_in_cart; //works percentage done.

    } elseif ($buy_any && $for_the_price) {
        if($quand_in_cart >= $buy_any){
            //here i need some help
        }
    }

1 个答案:

答案 0 :(得分:2)

以全价购买3并以Rest X支付2。

在mathamatics中它意味着:

$price = $prod_price * ((floor($quand_in_cart / $buy_any)*$for_the_price + ($quand_in_cart % $buy_any))

所以在你的代码中:

floor($quand_in_cart / $buy_any)*$for_the_price // for every 3, only add 2 to the price
($quand_in_cart % $buy_any) // how many items are the rest if you devide by $buy_any

编辑:更多解释

this.state.lists

添加这两个值,您必须将项目数量与一个项目的价格相乘。那就是它。