【问题标题】:Laravel 8 update product in cart if existsLaravel 8 更新购物车中的产品(如果存在)
【发布时间】:2021-10-24 14:37:05
【问题描述】:

我正在使用带有 darryldecode/laravelshoppingcart 购物车包的 Laravel 8。如果它存在于用户购物车中,我会在更新产品数量方面有点挣扎。 例如:如果购物车是空的,在添加产品时,然后在“添加到购物车”上单击多次,它只添加一个产品,然后将数量更新到最大。但是,当我添加第二个产品,然后再次添加产品 1 时,它会将另一个产品添加到购物车中。它绝对不应该添加另一个产品,但它必须限制在第一个产品的最大数量。

我的购物车商店方法:

    public function store(CartStoreRequest $request) {

        $validated = $request->validated();
        $product = Product::findOrFail($validated['product_id']);

        $rowId = uniqid();
        $userID = $validated['user_id'];

        // get current user cart
        $currentCart = \Cart::session($userID);

        if(!empty($currentCart->getContent()->toArray())) {
            foreach($currentCart->getContent() as $item) {
                $productID = $item->associatedModel->id;

                if($productID === $product->id) {
                    $currentCart->update($item->id, [
                        'quantity' => [
                         'relative' => false,
                         'value' => $product->minStock($item->quantity + $validated['quantity']),
                        ]
                    ]);
                } else {
                    $currentCart->add(array(
                        'id' => $rowId,
                        'name' => $product->name,
                        'price' => $product->price->amount(),
                        'quantity' => $validated['quantity'],
                        'associatedModel' => $product,
                        'attributes' => array(
                            'first_image' => $product->firstImage,
                            'formatted_price' => $product->formattedPrice,
                            'product_stock' => $product->stockCount()
                        )
                    ));
                }
            }
        } else {
            $currentCart->add(array(
                'id' => $rowId,
                'name' => $product->name,
                'price' => $product->price->amount(),
                'quantity' => $validated['quantity'],
                'associatedModel' => $product,
                'attributes' => array(
                    'first_image' => $product->firstImage,
                    'formatted_price' => $product->formattedPrice,
                    'product_stock' => $product->stockCount()
                )
            ));
        }

        return redirect()->back();
    }

我希望有人有类似的问题并且知道如何解决这个问题。

【问题讨论】:

    标签: php laravel cart shopping-cart


    【解决方案1】:

    好的,所以解决方法是使用产品ID作为$rowId,

            $validated = $request->validated();
            $product = Product::findOrFail($validated['product_id']);
    
            $rowId = $product->id;
            $userID = $validated['user_id'];
            $currentCart = \Cart::session($userID);
    
            $currentCart->add(array(
                'id' => $rowId,
                'name' => $product->name,
                'price' => $product->price->amount(),
                'quantity' => $product->minStock($validated['quantity']),
                'associatedModel' => $product,
                'attributes' => array(
                    'first_image' => $product->firstImage,
                    'formatted_price' => $product->formattedPrice,
                    'product_stock' => $product->stockCount()
                )
            ));
    
            $currentCartContents = $currentCart->get($rowId);
            $quantity = $product->minStock($currentCartContents->quantity);
    
            if($currentCartContents->quantity !== $quantity) {
                $currentCart->update($rowId, [
                    'quantity' => [
                     'relative' => false,
                     'value' => $quantity,
                    ]
                ]);
            }
    
            return redirect()->back();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-24
      • 2015-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多