laravelshoppingcart icon indicating copy to clipboard operation
laravelshoppingcart copied to clipboard

How to add products from only one attribute to the cart?

Open MindbGroup opened this issue 5 years ago • 0 comments

Is it possible to check if the products have the same attributes when adding to the cart?

For example, I have the shop_id attribute and I would like the basket to be created for only one shop_id, if another is added, the basket should be cleared and a new shop_id should be added.

below I am pasting my CartController file

`<?php

namespace App\Http\Controllers;

use App\Product; use Illuminate\Http\Request;

class CartController extends Controller { public function add(Product $product) {

    // Getting cart's contents for a specific user
    \Cart::Session(auth()->id())->add(array(
        'id' => $product->id,
        'name' => $product->name,
        'price' => $product->price,
        'quantity' => 1,
        'attributes' => array(
            'sd' => $product->shop_id
        ),
        'associatedModel' => $product
    ));


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

}

public function index()
{
    $cartItems = \Cart::Session(auth()->id())->getContent();
    return view('cart.index', compact('cartItems'));
}

public function destroy($itemId)
{
    \Cart::Session(auth()->id())->remove($itemId);
    return back();
}

public function update($rowId)
{
    \Cart::Session(auth()->id())->update($rowId,[
        'quantity' => array(
            'relative' => false,
            'value' => request('quantity')
        ),
    ]);
    return back();
}

public function checkout()
{
    return view('cart.checkout');
}

} `

MindbGroup avatar Nov 13 '20 11:11 MindbGroup