我如何做 "buy 3 and pay 2" 数学?
How do I do "buy 3 and pay 2" math?
我有一个购物车,我需要构建报价功能,我已经完成了百分比折扣,我需要一些关于数量促销的帮助。
例如,如果客户购买 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
}
}
用 Rest X 全价买 3 付 2。
在数学中它的意思是:
items_to_pay = floor(amount / 3) * 2 + X, with X = amount mod 3
因此在您的代码中:
$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
将这两个值相加,就得到了必须乘以一件商品价格的商品数量。就是这样。
我有一个购物车,我需要构建报价功能,我已经完成了百分比折扣,我需要一些关于数量促销的帮助。
例如,如果客户购买 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
}
}
用 Rest X 全价买 3 付 2。
在数学中它的意思是:
items_to_pay = floor(amount / 3) * 2 + X, with X = amount mod 3
因此在您的代码中:
$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
将这两个值相加,就得到了必须乘以一件商品价格的商品数量。就是这样。