【问题标题】:How to update cart in one function using Gloudemans\Shoppingcart in Laravel如何在 Laravel 中使用 Gloudemans\Shoppingcart 更新一个功能中的购物车
【发布时间】:2020-11-06 03:38:22
【问题描述】:

Gloudemans\Shoppingcart 模块有问题,可能有人在他的店里使用它,或者他正在使用它。

这两天,我一直在尝试编写一个代码,在一个 add 函数中更新我的卡,但它不起作用。

为什么这不起作用,我该如何解决?

public function add(Product $product, Request $request)
{
    $duplicates = Cart::search(function ($cartItem, $rowId) use ($product) {
        return $cartItem->id === $product->id;
    });

    if ($duplicates->isNotEmpty()) {

        MyFlash::success('Item is already in your cart!');

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

    Cart::add($product->id, $product->name, 1, $product->price)
        ->associate('App\Product');

    return redirect()->route('shop.cart.index')->with('success_message', 'Item was added to your cart!');
}

【问题讨论】:

标签: php laravel module cart shopping


【解决方案1】:

我相信你缺少的是 Buyable 界面,正如他们也提到的 here
“如果您的模型实现了 Buyable 接口并且您使用您的模型将商品添加到购物车,它将自动关联。”
可在Gloudemans\Shoppingcart\Contracts\Buyable中找到可购买界面;

<?php

namespace App\Product;

use Gloudemans\Shoppingcart\Contracts\Buyable;
use Illuminate\Database\Eloquent\Model;

class Product extends Model implements Buyable
{
   // ...your code...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-28
    • 1970-01-01
    • 2011-12-19
    • 2017-01-16
    • 1970-01-01
    相关资源
    最近更新 更多