代码之家  ›  专栏  ›  技术社区  ›  Mohammad

Laravel购物车应用程序无法按顺序显示项目总数

  •  0
  • Mohammad  · 技术社区  · 6 年前

    我正在开发购物车项目与拉维尔版本6.1.0。我想显示用户选择的总项目显示它购物车列表。问题是我可以将项目添加到列表中,但它不显示添加的项目列表。

    这是我的cart.php

    <?php
    namespace App;
    
    
    class Cart
    {
    
        public $items = null;
        public $totalQty = 0;
        public $totalPrice =0;
    
    
        public function _construct($oldCart)
        {
            if($oldCart)
            {
                $this->items = $oldCart->items;
                $this->totalQty = $oldCart->totalQty;
                $this->totalPrice = $oldCart->totalPrice;
    
            }
    
        }
        public function add($item, $id)
        { 
           $storedItem= ['qty' => 0, 'price' => $item->price,'item'=>$item];
    
             if($this->items){
    
            if(array_key_exists($id, $this->items)){
    
                $storedItem =$this->items[$id];
            }
        }
    
    
           $storedItem['qty']++;
           $storedItem['price'] = $item->price * $storedItem['qty'];
           $this->items[$id] = $storedItem;
           $this->totalQty++;
           $this->totalPrice += $item->price;
        }
    }
    

    这是我的控制器

    <?php
    
    namespace App\Http\Controllers;
    use App\Product;
    use Session;
    use App\Cart;
    use Illuminate\Http\Request;
    
    class ProductController extends Controller
    {
        public function getIndex(){
            $products = Product::all();
            return view('shop.index',['products' => $products]);
        }
    
      public function getAddToCart(Request $request, $id)
        {
            $product = Product::find($id);
            $cart = Session::has('cart') ? Session::get('cart') : null;
            if(!$cart)
            {
                $cart = new Cart($cart);
            }
            $cart->add($product, $product->id);
            $request->session()->put('cart',$cart); 
            // dd($request->session()->get('cart'));
            return redirect()->route('product.index');
        }
    
     public function getCart()
        {
            if(!Session :: has('cart') ){
                return view ('shop.shopping-cart');
            }
            $oldCart = Session::get('cart');
            $cart = new Cart($oldCart);
            return view ('shop.shopping-cart' , ['products' => $cart->items, 'totalPrice'=>$cart->totalPrice]);
        }
    
    
    }
    

    这是shopping-cart.blade.php

    @extends('layouts.master')
    
    @section('title')
        Laravel Shopping Cart
    @endsection
    
    @section('content')
         @if(Session::has('cart'))
         <div class="row">
         <div class="col-sm-6  col-md-6  col-md-offset-3  col-sm-offset-3">
         <ul class="list-group">
         @if(!empty($products) && count($products) > 0) 
         @foreach($products as $product) 
         <li class ="list-group-item">
         <span class ="badge"> {{ $product['qty'] }}</span>
         <strong >{{ $product['item']['title']}}</strong>
         <span class ="label label-success"> {{ $product['price']}}</span>
         <div class="btn-group">
         <button type="button" class="btn btn-primary btn-xs dropdown-toggle" data-toggle="dropdown">Action<span class="caret"></span></button>
         <ul class="dropdown-menu">
         <li> <a href="#"> Reduce By 1 </a> </li>
         <li> <a href="#"> Reduce All </a> </li>
         </ul>
         </div>
         </li>
         @endforeach
         @endif
         </ul>
         </div>
         </div>
    
         <div class ="row">
         <div class="col-sm-6 col-md-6 col-md-offset-3 col-sm-offset-3">
         <strong > Total : {{ $totalPrice}}</strong>
         </div>
         </div>
    
          <hr>
    
         <div class ="row">
         <div class="col-sm-6 col-md-6 col-md-offset-3 col-sm-offset-3">
         <button type="button" class="btn btn-success">Checkout</button>
         </div>
         </div>
    
    
         <div class ="row">
         <div class="col-sm-6 col-md-6 col-md-offset-3 col-sm-offset-3">
         <strong>Total : {{$totalPrice}} </strong>
         </div>
         </div>
         <hr>
         <div class ="row">
         <div class="col-sm-6 col-md-6 col-md-offset-3 col-sm-offset-3">
           <button type="button" class ="btn-btn-success">Checkout</button>
         </div>
         </div>
    
        @else
         <div class ="row">
         <div class="col-sm-6 col-md-6 col-md-offset-3 col-sm-offset-3">
         <h2>No Items In Cart</h2>
         </div>
         </div>
    
    
         @endif
    @endsection
    

    这是屏幕截图。 enter image description here

    1 回复  |  直到 6 年前
        1
  •  1
  •   waterloomatt    6 年前

    可能还有其他问题,但是 public function _construct() 应该是 public function __construct() 是的。注意双下划线。

    值得一提的是,看起来你跟这个也有同样问题的人在遵循同样的教程- Quantity of shopping cart is not updating 是的。