【问题标题】:Quantity of shopping cart is not updating购物车数量未更新
【发布时间】:2019-12-25 00:31:12
【问题描述】:

我正在学习 Laravel 的购物车教程,但即使我已经将他的代码复制到了重点,它也不能像教程中那样工作。每次你点击一个产品,购物车数量应该增加,但它没有增加。

我正在关注 Max Schwarzmueller 的“Laravel 购物车”教程,您可以在 youtube 上找到该教程:https://www.youtube.com/watch?v=56TizEw2LgI&list=PL55RiY5tL51qUXDyBqx0mKVOhLNFwwxvH。 我被困在#8。

我是 php、laravel 和会话的新手。我尝试过使用外观(例如 Session::put)而不是“$request->session()->put('cart', $cart)”。 使用“dd(session()->all())”打印会话确实确认正确的项目已添加到数组购物车中。

在 ProductController.php 中

<?php

namespace App\Http\Controllers;

use Session;
use App\Http\Controllers;
use App\Product;
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);

        //check in session if cart already contains product
        $oldCart = Session::has('cart') ? Session::get('cart') : null;

        //if it did contain products, pass them to constructor
        $cart= new Cart($oldCart);

        $cart->add($product, $product->id);

        $request->session()->put('cart', $cart);
        Session::save();

        return redirect()->route('product.index');
    }
}

在购物车.php 中

<?php

namespace App;

use Session;

class Cart 
{
    public $items = Array();
    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) {
        //default values if item not in cart 
        $storedItem = [
            'qty' => 0,
            'price' => $item->price,
            'item' => $item
        ];

        //if item is already in shopping cart
        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;
    }
}

每次点击产品都会激活 ProductController 的 getAddtoCart 功能

<a href="{{ route('product.addToCart', ['id'=> $product->id]) }}">Order</a>

我希望每次单击每个产品的“订单”时,购物车的总数量都会增加。 如果我在 Poke Ball 的“订单”上单击两次,则此特定项目(ID 为 1)的“数量”也应该增加。
但是 totalQty 从 0 开始,但不会增加超过 1。特定项目的 qty 也保持 1。

【问题讨论】:

  • 您是否尝试过转储整个会话以查看有关购物车的某些内容是否已更新?
  • @Mtxz 。我使用了“dd(session()->all()”,如果这就是你的意思。如果我点击 Poke Ball,它会显示在数组“items”中,但如果我点击 Great Ball,它会显示数组中只有此项;好像之前没有点击过 Poke 球。
  • 您不必调用Session::save() 来保存会话数据。我想知道您的会话是否有问题并且数据没有被持久化。您确定在请求期间没有在任何地方调用dd(...),这会过早结束请求并阻止会话数据持续存在吗?您能否确认您可以持久保存任何会话数据
  • 您使用的是什么会话驱动程序? 文件?如果您不确定,请查看config/session.php。最后,尝试清除浏览器缓存和 cookie。
  • @waterloomatt 。我已经删除了Session::save(),感谢您提供的信息。我已经检查过了,我在其他任何地方都没有 dd() 。会话驱动程序是'driver' =&gt; env('SESSION_DRIVER', 'file'),,它确实在我的存储会话文件夹中创建了一个文件。清除浏览器缓存和 cookie 没有帮助。数量确实重置为0,如果我点击“订单”,它确实会增加到1,但无论我多久再次点击“订单”,它都不会超过1

标签: php laravel session


【解决方案1】:

我花了一些时间才找到这个,但结果证明这是一个简单的语法错误,它把所有东西都扔掉了。您的购物车的构造函数使用单个下划线定义,并且应该使用双下划线定义。

由于 PHP 不需要使用构造函数,因此您合并新旧购物车的代码根本没有运行。 https://stackoverflow.com/a/455929/296555

// Wrong
public function _construct($oldCart){
   ...
}

// Right
public function __construct($oldCart){
   ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-17
    • 2021-04-19
    • 2021-01-29
    • 2012-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多