当前位置: 首页 > 知识库问答 >
问题:

Laravel购物车应用程序无法显示项目总质量

戚繁
2023-03-14

我正在用laravel 6.1.0版本开发购物车项目。我想显示用户选择的总项目显示购物车列表。问题是我能够将项目添加到列表中,但它没有显示添加的项目列表。。

这是我的购物车。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]);
    }


}

这是购物车。刀身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

共有1个答案

卓云
2023-03-14

可能还有其他问题,但公共函数构造()应该是公共函数构造()。请注意双下划线。

不管怎样,看起来你和这个也有同样问题的人遵循了同样的教程——购物车的数量没有更新。

 类似资料:
  • 我有一个订单按钮应该是重定向用户到购物车页面与订购的项目 这是web.php路线 这是addToCart函数 但是当我点击按钮时,它不会重定向到购物车页面,它会一直加载到同一个位置。 我尝试使用在addToCart函数上转储变量,它输出正确的结果

  • 本文向大家介绍Python实现购物车购物小程序,包括了Python实现购物车购物小程序的使用技巧和注意事项,需要的朋友参考一下 概要 按理说,我们入门的第一个小程序都应该是Hello World。因为比较简单,我这也就不做过多的演示 了。 下面是我写的一个小程序。主要用于练习Python的基本语法,以及入门。 主要实现功能 要求用户输入自己预期消费额度. 展示现有商品信息,要求用户选择 用户选择对

  • 问题内容: 我有上面的代码。 如果我在cart_item上运行print_r,我将得到一个多维数组: 我如何只获得product_id? 我试过$ test = 没用 问题答案: 要获取 foreach循环中的每个购物车商品(对于简单产品): 如果是可变产品,请获取 : 或在两种情况下 ( Woocommerce 3+中 的 Object在哪里) : 更新: 循环外使用产品ID 1)打破循环 (仅

  • 我有一个正常工作的函数,除了它在购物车中显示两次通知而不是一次。该函数应用折扣并显示通知。该函数查找特定类别的项目添加总数,如果满足折扣金额,则应用折扣并显示通知。现在,即使购物车里只有一件商品,它也会显示两次通知。 我已尝试添加,但这将清除我需要的其他通知,如类别上最小订单金额的最小-最大通知。如果我在函数开头或任何foreach语句中添加,它将在显示之前清除其他最小/最大通知。 以下是我已经看

  • 本文向大家介绍Python实现购物车程序,包括了Python实现购物车程序的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了程序:Python购物车程序,具体内容如下 需求: 启动程序后,让用户输入工资,然后打印商品列表 允许用户根据商品编号购买商品 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 可随时退出,退出时,打印已购买商品和余额 如余额不足,可充值  代码: 程序效

  • 本文向大家介绍Android购物车项目快速开发,包括了Android购物车项目快速开发的使用技巧和注意事项,需要的朋友参考一下 购物车项目,业务需要实现了一个购物车的项目,简单的了解下实现逻辑:数据计算等是在Adapter中计算出来的,通过在Adapter中计算出来的数据就可以回调到Activity中进行订单操作等功能业务逻辑,每一个店铺产生的数据是走一条流程的,(业务需求:不是作为一个类似淘宝,