php - laravel session 购物车 - 如果产品已经存在,如何增加数量

标签 php laravel session shopping-cart

我正在用 laravel 做一个在线购物项目, 但是这里有个问题, 我想如果用户将产品添加到购物车并且已经有相同的产品将添加数量金额

但在我的项目中:如果用户将产品添加到购物车并且已经拥有相同的产品,它将在 session 中添加一个新的值(value)项

像这样: enter image description here

数组:

array:3 [▼
  0 => array:5 [▼
    "id" => 7
    "nama_product" => "adssdsadxx"
    "harga" => 13
    "pict" => "s.gif"
    "qty" => 1
  ]
  1 => array:5 [▼
    "id" => 7
    "nama_product" => "adssdsadxx"
    "harga" => 13
    "pict" => "s.gif"
    "qty" => 1
  ]
  2 => array:5 [▼
    "id" => 7
    "nama_product" => "adssdsadxx"
    "harga" => 13
    "pict" => "s.gif"
    "qty" => 1
  ]
]

我想要这样(如果用户将产品添加到购物车并且已经有相同的产品将添加数量): enter image description here

数组:

array:1 [▼
  0 => array:5 [▼
    "id" => 7
    "nama_product" => "adssdsadxx"
    "harga" => 39
    "pict" => "s.gif"
    "qty" => "3"
  ]
]

这是我的 ProductsController.php:

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

    public function addToCart(Request $request, $id)
    {
        $product = DB::select('select * from products where id='.$id);
        $cart = Session::get('cart');
        $cart[] = array(
            "id" => $product[0]->id,
            "nama_product" => $product[0]->nama_product,
            "harga" => $product[0]->harga,
            "pict" => $product[0]->pict,
            "qty" => 1,
        );

        Session::put('cart', $cart);
        Session::flash('success','barang berhasil ditambah ke keranjang!');
        //dd(Session::get('cart'));
        return redirect()->back();
    }

    public function updateCart(Request $cartdata)
    {
        $cart = Session::get('cart');

        $cartQty = 1;

        foreach ($cartdata->all() as $id => $val) 
        {
            if ($cartQty != 1) {
                $cart[$id]['qty'] = $val;
                if ($val < 1) {
                    unset($cart[$id]);
                }
            }
            $cartQty++;
        }
        Session::put('cart', $cart);
        return redirect()->back();
    }

    public function deleteCart($id)
    {
        $cart = Session::get('cart');
        unset($cart[$id]);
        Session::put('cart', $cart);
        return redirect()->back();
    }

非常感谢...

最佳答案

如果产品已经存在则更新产品数量,否则将新产品添加到购物车中。

注意:确保 app/Http/Kernel.php 应该有 StartSession 进入 protected $middleware像这样。

enter image description here

 /**
     * Add product to the cart
     *
     * @return success message
     */
    public function addToCart(Request $request){
        $product = $request->all();

        $cart = Session::get('cart');

        /*
         * If product already exist into the cart then update QTY of product
         * Othewise add new product into the cart
         */
        if(isset($cart[$product['id']])):
            $cart[$product['id']]['qty'] += 1;
        else:
            $cart[$product['id']] = $product;
            $cart[$product['id']]['qty'] = 1; // Dynamically add initial qty
        endif;

        Session::put('cart', $cart);

        return Response::json(['success' => true, 'cart_items' => count(Session::get('cart')), 'message' => 'Cart updated.']);
    }

关于php - laravel session 购物车 - 如果产品已经存在,如何增加数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47136383/

相关文章:

php - 使用 MAMP 的 Laravel 错误 HTTP 500

php - 将客户的邮政地址存储在 cookie 中是一种好的做法吗?

php - 在 Laravel 中检索 session 值返回 null

php - JSON 数组未设置

laravel - 将mysql中的日期解析为carbon对象,然后转换为本地时区

php - 使用 hasColumns laravel 更改数据库表

php - Laravel - 仅针对当前 session 附加到身份验证用户

javascript - 如何替换文本框中的逗号?

php - 如何让unix时间戳显示超过24小时的时间?

php - Symfony UniqueEntity 在更新现有实体时显示错误