简体   繁体   中英

Add and update products to session cart in Laravel

I am building a shopping cart for guests with sessions in laravel. I can add one product with quantity to the session variable 'cart.' However, when I am adding the same product again, I am unable to increment quantity as I am unable to search for the same product ID in the array.

Controller

public function addToSessionCart(Request $request, $id)
{
    if (session()->has('cart')) {
        $oldCart = session()->get('cart');
        $cartdata = $oldCart;
        if (array_key_exists('product_id', $cartdata)) {
            if (in_array($id, $cartdata['product_id'], true)) {
                $cartdata['quantity'] += 1;
            } else {
                $cartdata['product_id'] = $id;
                $cartdata['quantity'] = 1;
            }
        }
    } else {
        $cartdata = [];
        $cartdata['product_id'] = $id;
        $cartdata['quantity'] = 1;
    }
    session()->put('cart', $cartdata);
    session()->flash('message', 'Item added to cart !');

    return back();
}

I am passing data to cart from my AppServiceProvider:

view()->composer('*', function ($view) {
    if (Auth::check()) {
        $cart_items = Cart::where('user_id', Auth::user()->id)->with('product')->get();
        $cart_items_count = Cart::where('user_id', Auth::user()->id)->count();
        $menus = Menu::where('status', '1')->orderBy('order', 'ASC')->get();
        $view->with(['cart_items' => $cart_items, 'cart_items_count' => $cart_items_count, 'menus' => $menus]);
    } else {
        $cart_items = null;
        $session_cart_items = null;
        if (\Session::get('cart')) {
            $session_cart_items = [];
            foreach (session('cart') as $session_cart_items[]) {
                $session_cart_items['product'] = Product::where('id', session('cart.product_id'))->get();
                $session_cart_items['quantity'] = session('cart.quantity');
            }
        }

        $cart_items_count = count(\Session::get('cart'));
        $menus = Menu::where('status', '1')->orderBy('order', 'ASC')->get();
        $view->with([
            'cart_items' => $cart_items, 'cart_items_count' => $cart_items_count, 'menus' => $menus,
            'session_cart_items' => $session_cart_items,
        ]);
    }
}); 

View

@foreach($session_cart_items['product'] as $key => $name)
    <div class="product-cart">
        <div class="product-image">
            <img src="/frontend/assets/images/{{ $name->image }}" alt="image">
        </div>
        <div class="product-content">
            <h3><a href="/product/{{$name->id}}/{{$name->slug}}">{{$name->name}}</a></h3>
            <div class="product-price">
                <span>{{ $session_cart_items['quantity'] }}</span>
                <span>x</span>
                &#8377; <span class="price">{{ $name->price }}</span>
            </div>
        </div>
    </div>
    @php
        $total += ($name->price * $session_cart_items['quantity']);
    @endphp
@endforeach

I want the quantity to be incremented when the same product gets added to the cart. If not then add a new product id and quantity to the session array

This line needs to be changed:

foreach (session('cart') as $session_cart_items[]) {

You cannot use [] for reading.

foreach (session('cart') as $session_cart_items) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM