duanci1858 2017-10-01 04:46
浏览 50
已采纳

购物车总额包邮

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

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);
}
  • 写回答

2条回答 默认 最新

  • duanao2688 2017-10-01 04:52
    关注

    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. try below code

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

    EDIT You can achieve this using sql query also :

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教