我一直试图根据产品数量找出总价,
我正在数据库中保存数量和价格,以下是我用来查找总价的查询

SELECT SUM( price * quantity ) AS subtotal, SUM( quantity ) AS qty
FROM  `cart`
WHERE user
IN (

SELECT id
FROM users
WHERE email =  'test'
)


现在我想要的是,我需要添加运费,如果数量是1-5,则运费将是50,如果数量是6-10,则运费将是100,等等。

我怎样才能做到这一点?这是我正在尝试但错误的!请给我找到解决方案。

 $subtotalquery=" SELECT SUM( price * quantity ) as subtotal, SUM(quantity) as qty FROM  `cart` WHERE user IN  (select id from users where email='$user_check')";
                                $t_con=$conn->query($subtotalquery);
                                $subtotalrow = $t_con->fetch_assoc();

                                $subtotal= $subtotalrow['subtotal'];
                                $qty= $subtotalrow['qty'];
                                if($qty>=5)
                                {
                                    $shipping=50 ;

                                    $ithship=$subtotalrow+($shipping);

                                }else
                                {
                                $shipping=50*2 ;

                                    $ithship=$subtotalrow+($shipping*2);
}

最佳答案

尝试以下代码,您需要将运费加到小计中

if($qty<=5)
{
    $shipping=50 ;
}else
{
$shipping=50*2 ;
}
$ithship=$subtotal+$shipping; // add to  subtotal


编辑

如果要增加每5个以上数量的运费。尝试下面的代码

$qty= $subtotalrow['qty'];
$incr  = ceil($qty/5);
$shipping = $incr*50;
echo $shipping;


编辑
您也可以使用sql查询来实现:

SELECT SUM( price * quantity ) as subtotal, SUM(quantity) as qty, ((ceil(SUM(quantity)/5))*50) as ShippingCharge FROM  `cart` WHERE user IN  (select id from users where email='$user_check');


DEMO

关于php - 购物车总计含运费,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46509308/

10-11 03:14