How to store the data added to the shopping cart in the cookie

How to store the data added to the shopping cart in the cookie

Imagine our shopping cart record:

How to store the data added to the shopping cart in the cookie

It seems that the data structure is not particularly simple. There are product thumbnails, titles, attributes (not considered for the time being), prices, quantities, etc. and there are multiple products

Generally, we naturally think of assembling the data into a two-dimensional array, but the array cannot be written into the cookie, it doesn't matter, we can first assemble it into an array, and then serialize the array, then the cookie can not be written.

[php]
$goodsArr=[
‘1(商品id)’=>[
‘goods_name’=>’测试’,
‘price’=>100,
‘thumb’=>’http://tongpankt.com/logo.png’,
‘num’=>10
],
‘2(商品id)’=>[
‘goods_name’=>’测试’,
‘price’=>200,
‘thumb’=>’http://tongpankt.com/logo.jpg’,
‘num’=>20
],
];
[/php]

There is no problem with this idea as a whole, but the logical details are a little bit problematic.

For example, for a product, its title, thumbnail, and price are not fixed. For example, if we want to modify the price of a product during a festival or a store event, then if you have written the price into the cookie, it cannot be dynamic. Changed, so we don’t need to save such a complicated data format at all, we only need to save one as an array:

[php]
$goodsArr=[
'Product id'=>'Product quantity',
'1'=>'10',
'2'=>'20',
];
[/php]

Other data that may change, such as pictures, titles, prices, etc., when we read it, we first deserialize the cookie data to get the product id and quantity, and we can get it dynamically based on the product id.

Guess you like

Origin blog.csdn.net/weixin_45557228/article/details/109574377