dquv73115 2019-02-22 06:04
浏览 177
已采纳

如果数量小于0,如何从购物车中删除商品。 Laravel

My problem is that the item is not removed from cart when quantity is less than 0 and also quantity is going -1,-2 etc. Can you help me?

This is my controller

 public function reducequantity(Request $request){
    $id = $_GET['id']; 
    $oldcart = Session::has('cart') ? Session::get('cart') : null;
    $cart = new Cart($oldcart);
    $cart->reducequantity($id);
    Session::put('cart',$cart);      
    echo json_encode("item reduced from cart");
    }

This is my model

    public function reducequantity($id){       
        $this->items[$id]['qty']--;
        $this->items[$id]['price'] -=  $this->items[$id]['item']['price'];
        $this->totalqty--;
        $this->total_price -=  $this->items[$id]['item']['price'];
        if($this->items[$id]['qty'] <= 0){ 
            unset($this->items[$id]);
        }       
   } 
  • 写回答

1条回答 默认 最新

  • douxianwu2221 2019-02-22 10:08
    关注
        **Solved By making a little change in controller**
        public function removeitem(Request $request){
         $id = $_GET['id']; 
         $oldcart = Session::has('cart') ? Session::get('cart') : null;
         $cart = new Cart($oldcart);
          $cart->removeitem($id);
           if(count($cart->items) > 0){
             Session::put('cart',$cart);
         }else{
            Session::forget('cart');
         }
    
          echo json_encode("item removed");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出