简体   繁体   English

带有 Laravel 的 PHP 购物车 - 数量问题

[英]PHP shopping cart with laravel - quantity issue

I am working on a laravel-shopping site project, and I have problems with the quantity of the product in the cart.我正在处理一个 Laravel 购物网站项目,我对购物车中的产品数量有疑问。 I am relatively new to Laravel, so things gets more complicated for me... The thing is that I can't really loop (and look for) simular products in a effective way.我对 Laravel 比较陌生,所以对我来说事情变得更加复杂......问题是我无法真正以有效的方式循环(并寻找)模拟产品。

But when I press the "Add to cart"-button, this function will run:但是当我按下“添加到购物车”按钮时,这个函数会运行:

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

The products are objects, and I am not sure how to loop to find the id of the product to match the array-products id.产品是对象,我不确定如何循环查找产品的 id 以匹配 array-products id。 I tried json_decode() to see if that would make it into an array, but I might be wrong here because when I return the value it was the same "object".我试过 json_decode() 来看看它是否会变成一个数组,但我在这里可能是错的,因为当我返回值时它是同一个“对象”。 For example a single product in the cart can look like this:例如,购物车中的单个产品可能如下所示:

    [{"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"
}]

You need to run a foreach loop, this will loop through all the objects.您需要运行一个 foreach 循环,这将遍历所有对象。

So for example you can run this code in a controller to get the id :例如,您可以在controller运行此代码以获取id

foreach($products as $product) {
    dump($product->id);
}

Or you can use this in a blade view .或者您可以在blade view使用它。

@foreach($products as $product)
    dump($product->id);
@endforeach

I suggest you to add a quantity in your object, than you can update the quantity and calculate the price.我建议您在对象中添加一个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();
}

Hope this works!希望这有效!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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