【发布时间】:2021-07-23 00:08:44
【问题描述】:
我有一个存储在 cookie 中的产品列表,名称为 shopping_cart 为 JSON。
我想做什么?
我正在尝试更新产品的数量并将其保存到 cookie。除了删除所有 cookie 并使用更新的数据创建一个新的 cookie(同名)之外,我想不出其他方法。 步骤是:
- 获取 cookie 数据并将其存储在变量 $items 中。
- 遍历所有项目,并通过检查与之相关的 hash_id 来检查其中一项是否是更新其数量的项目。
- 删除旧 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"
]
]
【问题讨论】: