簡體   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