在Laravel中将产品添加和更新到会话购物车

时间:2019-08-30 18:18:39

标签: php laravel e-commerce cart shopping-cart

我正在为在laravel中开会的来宾建立购物车。 我可以将一种带有数量的产品添加到会话变量“购物车”中。但是,当我再次添加相同的产品时,由于无法在数组中搜索相同的产品ID,因此无法增加数量。

控制器

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();
}

我正在将数据从我的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,
        ]);
    }
}); 

查看

@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

当同一产品添加到购物车时,我希望数量增加。如果没有,请向会话数组添加新的产品ID和数量

1 个答案:

答案 0 :(得分:0)

此行需要更改:

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

您不能使用[]进行阅读。

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