laravel session 购物车 - 如果产品已经存在,如何增加数量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47136383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
laravel session shopping cart - how to increase the quantity if the product already exists
提问by Tofa Maulana Irvan
i'm making an online shopping project with laravel, But there is a problem here, I want if the user adds the product to the shopping cart and there is already the same product will be added quantity amount,
我正在用laravel做一个在线购物项目,但是这里有一个问题, 我想如果用户将产品添加到购物车并且已经有相同的产品将被添加数量金额,
but on my project: if a user adds a product to the shopping cart and already has the same product, it will add a new value item to the session
但在我的项目中:如果用户将产品添加到购物车并且已经拥有相同的产品,它将向会话添加一个新的价值项目
array :
大批 :
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
]
]
I want it like this (if the user adds the product to the shopping cart and there is already the same product will be added quantity amount):
我想要这样(如果用户将产品添加到购物车并且已经有相同的产品将添加数量金额):
array :
大批 :
array:1 [▼
0 => array:5 [▼
"id" => 7
"nama_product" => "adssdsadxx"
"harga" => 39
"pict" => "s.gif"
"qty" => "3"
]
]
this is my ProductsController.php:
这是我的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();
}
Thank you very much...
非常感谢...
回答by Mayank Dudakiya
Update product quantity if product already exist, otherwise add new product into the cart.
如果产品已经存在,则更新产品数量,否则将新产品添加到购物车中。
Note:Make sure app/Http/Kernel.php
should have StartSession
into the protected $middleware
like this.
注:确保选中app/Http/Kernel.php
应该StartSession
到protected $middleware
这个样子。
/**
* 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.']);
}
回答by Lexxusss
you need to change a bit your code:
你需要稍微改变一下你的代码:
public function addToCart(Request $request, $id)
{
$product = DB::select('select * from products where id='.$id);
$cart = Session::get('cart');
$cart[$product[0]->id] = 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');
foreach ($cartdata->all() as $id => $val)
{
if ($val > 0) {
$cart[$id]['qty'] += $val;
} else {
unset($cart[$id]);
}
}
Session::put('cart', $cart);
return redirect()->back();
}
just create cart array keyby-ing it by product ID.
只需按产品 ID 键创建购物车数组即可。