购物车数量未更新 [英] Quantity of shopping cart is not updating

查看:60
本文介绍了购物车数量未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注Laravel的购物车教程,但是即使我已经将他的代码复制到了要点,也无法像在教程上那样工作.每次您点击产品时,购物车数量应该增加,但是没有增加.

I am following a shopping cart tutorial with Laravel, but even though I've copied his code to the point, it does not work like on the tutorial. Every time you click on a product, the shopping cart quantity SHOULD increment, but it didn't.

我正在遵循Max Schwarzmueller的"Laravel购物车"教程,您可以在youtube上找到它: https://www.youtube.com/watch?v=56TizEw2LgI&list=PL55RiY5tL51qUXDyBqx0mKVOhLNFwwxvH .我被困在#8.

I am following the tutorial of Max Schwarzmueller's "Laravel Shopping Cart" which you can find on youtube at: https://www.youtube.com/watch?v=56TizEw2LgI&list=PL55RiY5tL51qUXDyBqx0mKVOhLNFwwxvH. I am stuck at #8.

我是php,laravel和session的新手.我试过使用立面(例如Session :: put),而不是"$ request-> session()-> put('cart',$ cart).使用"dd(session()-> all())"打印会话可以确认已将正确的商品添加到阵列购物车中.

I am new with php, laravel and sessions. I've tried using facades (e.g. Session::put) instead of "$request->session()->put('cart', $cart)". Printing the session with "dd(session()->all())" does confirm that the correct item got added to the array shopping cart.

在ProductController.php中

In 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');
    }
}

在Cart.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函数

Every time I click on product it activated the getAddtoCart function of ProductController

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

我希望每当我单击每种产品的订购"时,购物车的totalQty应该增加.如果我在Poke Ball的订购"上单击两次,则该特定商品(ID为1)的数量"也应增加.
但是totalQty从0开始,但增量不超过1.此外,特定项目的qty仍为1.

I expect that the totalQty of shopping cart should be incremented every time I click on "order" of every products. If I click twice on "order" for Poke Ball, the "qty" for this specific item (with id 1) should also increment.
But the totalQty starts from 0, but does not increment more than 1. Also qty for the specific item remains 1.

推荐答案

花了我一段时间才找到它,但事实证明这是一个简单的语法错误,已使所有内容一无所有.您购物车的构造函数使用单个下划线定义,应使用双下划线定义.

Took me a while to find this but it turns out to be a simple syntax error that is throwing everything off. Your cart's constructor is defined with a single underscore and should be defined using double-underscore.

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

Since PHP doesn't require the use of a constructor, your code to merge the old/new carts simply wasn't running. https://stackoverflow.com/a/455929/296555

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

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

这篇关于购物车数量未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆