我该如何做“买3并付2”数学?
问题描述:
我有一个购物车,我需要建立提供功能,我已经完成了百分比,我需要一些数量促销帮助。我该如何做“买3并付2”数学?
例如,如果客户购买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
}
}
答
买3个,并支付2休息X全价。
在mathamatics它意味着:
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
添加这两个值,你有必须与价格相乘的项目数量为一个项目。而已。
开始具有最高数量检查第一:'如果(数量> = 5){}否则如果(数量> = 3)...'' – aynber
$ FULLPRICE = $ quand_in_cart%$ buy_any; $ discounted = $ quand_in_cart - $ fullPrice;' –