繁体   English   中英

如何更新购物车中 cookie 中存储的产品数量?

[英]How to update product quantity stored in cookies in shopping cart?

我有一个存储在 cookie 中的产品列表,名称为shopping_cart作为 JSON。

我想做什么?

我正在尝试更新产品的数量并将其保存到 cookie。 除了删除所有cookie并使用更新的数据创建一个新的cookie(具有相同名称)之外,我想不出其他方法。 步骤是:

  1. 获取 cookie 数据并将其存储在变量 $items 中。
  2. 循环遍历所有项目并通过检查与其相关的 hash_id 来检查其中一个是否是更新其数量的项目。
  3. 删除旧 cookie 并创建一个具有相同旧名称的新 cookie。

我面临的问题是什么?

我遇到的问题是旧会话没有被删除(使用后: Cookie::queue(Cookie::forget('shopping_cart')); )并且在执行代码后,新的更新产品(数量更新)是用旧产品创建的;

示例:下面的示例是 json_decode 并使用“dd($cart_data);”后cookie“shopping_cart”中存在的产品的输出。

array:2 [▼
  4 => array:8 [▼
    "product_uuid" => "727b8f0a-6925-4ecd-b9e0-3bc3e8a08a9a"
    "product_name" => "قميص جينز سادة جينز أزرق وسط22"
    "product_image" => "41d9f176-cacb-486b-a901-53ed0e67447d.jpg"
    "product_quantity" => "6"
    "product_price" => "7811"
    "original_price" => "7600.00"
    "attributes" => array:2 [▼
      "25a5b207-fe0f-482d-b6fc-3034f7db179e" => "bda60283-0490-4ed3-847c-39ec03f28796"
      "c8cc3ce4-fdd7-4368-9cf2-4bf5f5c24497" => "d2290947-5e5d-4d02-8f02-19c8269698bd"
    ]
    "hash_id" => "$2y$10$qwRmwtxBHqnAS4ef.no1i.x/.zVbG4gmyz0e3laq86eqS94JbUMmu"
  ]
  5 => array:8 [▼
    "product_uuid" => "cd048d7f-462a-40ac-b9ed-49a8c499da06"
    "product_name" => "الحذاء الرياضي تشارجد كوميت 2 أسود / أبيض"
    "product_image" => "1625987228.png"
    "product_quantity" => "1"
    "product_price" => "4400"
    "original_price" => "4000.00"
    "attributes" => array:2 [▼
      "f015fee9-1e17-447c-bb12-6f6f55588001" => "2be4e09b-62a8-47bf-bba1-257fbab55cce"
      "8d318964-09ca-4770-9dd5-c75ff29f03cb" => "5864acfd-c0fd-498f-bdd6-6b6615eabb65"
    ]
    "hash_id" => "$2y$10$0PZobkhbEJsdf52n0f2HSertloWiQOHchFOCqgGbLr9itc.nAt4SG"
  ]
]

我更新产品数量的代码:

  if (Cookie::get('shopping_cart')) {
            $cookie_data = stripslashes(Cookie::get('shopping_cart'));

            $cart_data = json_decode($cookie_data, true);


            $quantity = $request->quantity;
            $hash_id = $request->hash_id;

            $items = $cart_data;


            $productsArray = [];
            foreach ($items as $item) {

                if ($item['hash_id'] == $hash_id)
                {
                    $productsArray[] = [
                        'product_uuid' => $item['product_uuid'],
                        'product_name' => $item['product_name'],
                        'product_image' => $item['product_image'],
                        'product_quantity' => $quantity,
                        'product_price' => $item['product_price'],
                        'original_price' => $item['original_price'],
                        'attributes' => $item['attributes'],
                        'hash_id' => $item['hash_id'],
                    ];

                }
                if ($item['hash_id'] != $hash_id)
                {
                    $productsArray[] = [
                        'product_uuid' => $item['product_uuid'],
                        'product_name' => $item['product_name'],
                        'product_image' => $item['product_image'],
                        'product_quantity' =>  $item['product_quantity'],
                        'product_price' => $item['product_price'],
                        'original_price' => $item['original_price'],
                        'attributes' => $item['attributes'],
                        'hash_id' => $item['hash_id'],
                    ];
                }

            }


            Cookie::queue(Cookie::forget('shopping_cart'));

            foreach ($productsArray as $product)
            {

                $cart_data[] = $product;

                $product_data = json_encode($cart_data, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE);
                Cookie::queue(Cookie::make('shopping_cart', $product_data,7200));
            }

            $totalcart = count($cart_data);

            $order = "";
            return view('guest.cart.index', compact('cart_data', 'totalcart','order'));
        }

代码执行后的结果:

array:4 [▼
  4 => array:8 [▼
    "product_uuid" => "727b8f0a-6925-4ecd-b9e0-3bc3e8a08a9a"
    "product_name" => "قميص جينز سادة جينز أزرق وسط22"
    "product_image" => "41d9f176-cacb-486b-a901-53ed0e67447d.jpg"
    "product_quantity" => "6"
    "product_price" => "7811"
    "original_price" => "7600.00"
    "attributes" => array:2 [▶]
    "hash_id" => "$2y$10$qwRmwtxBHqnAS4ef.no1i.x/.zVbG4gmyz0e3laq86eqS94JbUMmu"
  ]
  5 => array:8 [▼
    "product_uuid" => "cd048d7f-462a-40ac-b9ed-49a8c499da06"
    "product_name" => "الحذاء الرياضي تشارجد كوميت 2 أسود / أبيض"
    "product_image" => "1625987228.png"
    "product_quantity" => "1"
    "product_price" => "4400"
    "original_price" => "4000.00"
    "attributes" => array:2 [▶]
    "hash_id" => "$2y$10$0PZobkhbEJsdf52n0f2HSertloWiQOHchFOCqgGbLr9itc.nAt4SG"
  ]
  6 => array:8 [▼
    "product_uuid" => "727b8f0a-6925-4ecd-b9e0-3bc3e8a08a9a"
    "product_name" => "قميص جينز سادة جينز أزرق وسط22"
    "product_image" => "41d9f176-cacb-486b-a901-53ed0e67447d.jpg"
    "product_quantity" => "8"
    "product_price" => "7811"
    "original_price" => "7600.00"
    "attributes" => array:2 [▶]
    "hash_id" => "$2y$10$qwRmwtxBHqnAS4ef.no1i.x/.zVbG4gmyz0e3laq86eqS94JbUMmu"
  ]
  7 => array:8 [▼
    "product_uuid" => "cd048d7f-462a-40ac-b9ed-49a8c499da06"
    "product_name" => "الحذاء الرياضي تشارجد كوميت 2 أسود / أبيض"
    "product_image" => "1625987228.png"
    "product_quantity" => "1"
    "product_price" => "4400"
    "original_price" => "4000.00"
    "attributes" => array:2 [▶]
    "hash_id" => "$2y$10$0PZobkhbEJsdf52n0f2HSertloWiQOHchFOCqgGbLr9itc.nAt4SG"
  ]
]

我发现了导致我出现此类问题的错误。 这是因为我重复了变量 $cart_data 并且在将新项目保存到 cookie 之前忘记更改它,解决方案是更改变量名称。

暂无
暂无

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

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