PHP 购物车 laravel - 数量问题
PHP shopping cart with laravel - quantity issue
我正在做一个 laravel 购物网站项目,我对购物车中的产品数量有疑问。
我对 Laravel 比较陌生,所以事情对我来说变得更加复杂......
问题是我无法真正有效地循环(并寻找)类似的产品。
但是当我按下 "Add to cart" 按钮时,这个函数将 运行:
public function cart(Product $product){
$cart = session()->get('cart');
// If Cart is empty, add a new array in the session
if ($cart == []) {
$cart[] = $product;
session()->put('cart', $cart);
}else{
// else if not empty, loop the cart and look if similar product is in the cart.
for ($i = 0; $i <= count($cart); $i++) {
$array_cart = json_decode($cart[$i], true);
if ($product->id == $array_cart["id"]) {
$array_cart["id"] += 1;
}else{
$cart[] = $product;
session()->put('cart', $cart);
}
}
}
return back();
}
产品是对象,我不确定如何循环查找产品的 ID 以匹配数组产品 ID。我尝试 json_decode() 看看是否可以将其放入数组,但我在这里可能是错误的,因为当我 return 时,它的值是相同的 "object"。例如,购物车中的单个产品可能如下所示:
[{"id": 4,
"name": "HP Desktop",
"description": "Ordinary desktop",
"category": "Desktop",
"price": 1,
"views": 63,
"created_at": "2016-04-11 14:42:58",
"updated_at": "2016-05-27 09:12:59"
}]
您需要 运行 一个 foreach 循环,这将遍历所有对象。
例如,您可以 运行 controller
中的此代码以获得 id
:
foreach($products as $product) {
dump($product->id);
}
或者您可以在 blade view
.
中使用它
@foreach($products as $product)
dump($product->id);
@endforeach
我建议您在对象中添加一个 quantity
,这样您就可以更新 quantity
并计算价格。
public function cart(Product $product){
$cart = session()->get('cart');
// If Cart is empty, add a new array in the session
if ($cart == []) {
$cart[] = $product;
session()->put('cart', $cart);
}else{
// else if not empty, loop the cart and look if similar product is in the cart.
foreach ($cart as $product_item) {
if ($product->id == $product_item["id"]) {
$product_item["quantity"] += 1;
$found = true;
}
}
if($found !== true) {
$cart[] = $product;
session()->put('cart', $cart);
}
}
return back();
}
希望这有效!
我正在做一个 laravel 购物网站项目,我对购物车中的产品数量有疑问。 我对 Laravel 比较陌生,所以事情对我来说变得更加复杂...... 问题是我无法真正有效地循环(并寻找)类似的产品。
但是当我按下 "Add to cart" 按钮时,这个函数将 运行:
public function cart(Product $product){
$cart = session()->get('cart');
// If Cart is empty, add a new array in the session
if ($cart == []) {
$cart[] = $product;
session()->put('cart', $cart);
}else{
// else if not empty, loop the cart and look if similar product is in the cart.
for ($i = 0; $i <= count($cart); $i++) {
$array_cart = json_decode($cart[$i], true);
if ($product->id == $array_cart["id"]) {
$array_cart["id"] += 1;
}else{
$cart[] = $product;
session()->put('cart', $cart);
}
}
}
return back();
}
产品是对象,我不确定如何循环查找产品的 ID 以匹配数组产品 ID。我尝试 json_decode() 看看是否可以将其放入数组,但我在这里可能是错误的,因为当我 return 时,它的值是相同的 "object"。例如,购物车中的单个产品可能如下所示:
[{"id": 4,
"name": "HP Desktop",
"description": "Ordinary desktop",
"category": "Desktop",
"price": 1,
"views": 63,
"created_at": "2016-04-11 14:42:58",
"updated_at": "2016-05-27 09:12:59"
}]
您需要 运行 一个 foreach 循环,这将遍历所有对象。
例如,您可以 运行 controller
中的此代码以获得 id
:
foreach($products as $product) {
dump($product->id);
}
或者您可以在 blade view
.
@foreach($products as $product)
dump($product->id);
@endforeach
我建议您在对象中添加一个 quantity
,这样您就可以更新 quantity
并计算价格。
public function cart(Product $product){
$cart = session()->get('cart');
// If Cart is empty, add a new array in the session
if ($cart == []) {
$cart[] = $product;
session()->put('cart', $cart);
}else{
// else if not empty, loop the cart and look if similar product is in the cart.
foreach ($cart as $product_item) {
if ($product->id == $product_item["id"]) {
$product_item["quantity"] += 1;
$found = true;
}
}
if($found !== true) {
$cart[] = $product;
session()->put('cart', $cart);
}
}
return back();
}
希望这有效!