简体   繁体   English

购物车总计含运费

[英]Cart Total with Shipping

I have been trying to find out the total price based on the product quantity, I am saving quantity and price in database following is the query that i am using to find out the total price 我一直试图根据产品数量找出总价,我正在数据库中保存数量和价格,以下是我用来找出总价的查询

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

SELECT id
FROM users
WHERE email =  'test'
)

Now what i want , i need add shipping charges, if the quantity is 1-5 than shipping charges would be 50 and if 6-10 than it would be 100 ad so on 现在我想要的是,我需要添加运费,如果数量是1-5,则运费将是50,如果数量是6-10,则运费将是100,等等。

How can i achieve this ? 我怎样才能做到这一点? this is what i am trying but wrong! 这是我正在尝试但错误的! Please find me a solution. 请给我找到解决方案。

 $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);
}

You could use CASE statement instead 您可以改用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'
)

>>>Demo<<< >>>演示<<<

Try below code, You need to add shipping charge into subtotal 尝试以下代码,您需要将运费加到小计中

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

EDIT 编辑

if you want to increase shipping charge on every 5+ qty. 如果要增加每5个以上数量的运费。 try below code 尝试下面的代码

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

EDIT You can achieve this using sql query also : 编辑您也可以使用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 DEMO

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

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