买一个一半的价格在PHP购物车总计
我正在PHP中创建一个购物车,一个特定的项目是买一个得到一半的价格。当用户购买物品时,我会得到要从总数中扣除的优惠,但我坚持如何以数学方式进行此操作。买一个一半的价格在PHP购物车总计
到目前为止,我有一个,如果循环得到的数据是这样的,从数据库:
$total = $total+($arraycart['Price']*$quantity);
那么我认为这将是沿着线的东西:
if ($arraycart['Item'] == "A1" and $quantity > 1) {
//calculate here buy one get one half price
}
任何帮助表示赞赏。
<?php
$total = 0;
$arraycart['Price'] = 10;
$arraycart['Item'] = 'A1';
$quantity = 3; // change item quantity here
if ($arraycart['Item'] == "A1" and $quantity % 2 == 0) {
//calculate here buy one get one half price
$real = ($quantity/2)*$arraycart['Price'];
$half = ($quantity/2)*($arraycart['Price']/2);
$total = $real+$half;
} else {
$quantity = $quantity-1;
$real = ($quantity/2)*$arraycart['Price'];
$half = ($quantity/2)*($arraycart['Price']/2);
$total = $real+$half+$arraycart['Price'];
}
echo $total;
?>
第一次购物后,您可以享受50%的折扣优惠,而不仅仅是买一赠一,这意味着每一次以全价购买,下一次是一半。如果数量是5,那么当它应该只在2时,你给予4个折扣。 –
如果(和$数量
因此,如果您订购4 ,你没有得到折扣? –
'$ total = $ total +((0.5 * $ arraycart ['Price'])* $ quantity);'? –
@u_mulder会增加一切的一半价格。他们已经在计算完整的价格时间数量。你不希望再增加一半的价格。你更可能想要减去一半数量的一半价格,例如:'$ total = $ total - ($ arraycart ['Price'] * 0.5 * floor($ quantity/2))''这会将半价格减半的项目。这当然是在你做'$ total'数学之后。 –
我刚刚给了个提示。检查何时应用它是OP的任务 –