繁体   English   中英

在PHP购物车中购买一对一的总价

[英]Buy One Get One Half Price in PHP shopping cart total

我正在用PHP创建购物车,一个特殊的项目是买一赠半的价格。 当用户购买商品时,我希望从总价中扣除要约,但我仍然坚持如何数学上做到这一点。

到目前为止,在从数据库中获取数据的if循环中,我有类似的内容:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM