[英]Cart Total with Shipping
我一直试图根据产品数量找出总价,我正在数据库中保存数量和价格,以下是我用来找出总价的查询
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);
}
您可以改用CASE
语句
SELECT SUM(price * quantity) AS subtotal,
SUM(quantity) as quantity,
CASE
WHEN SUM(quantity) <=5 THEN
50
WHEN SUM(quantity) BETWEEN 6 AND 10 THEN
100
ELSE
150
END as ShippingCharge
from `cart`
WHERE user
IN
(
SELECT id
FROM users
WHERE email = 'test'
)
尝试以下代码,您需要将运费加到小计中
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');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.