[英]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.