购物车会话数组

发布于 2024-12-28 01:55:51 字数 420 浏览 0 评论 0原文

我正在构建一个简单的购物车,并且从产品页面获取以下数组(当用户提交“添加到购物车”按钮时)...

Array ( [product_id] => 1 [weight] => 2 [size] => 4 [type] => cafe [cost] => 6323.86 )

之后,我将其保存到 session 以便稍后显示购物车...

$_SESSION['cart']= $data; //$_POST values into $data

如果用户仅将 1 个产品添加到购物车中,但如果他添加另一个产品,$_SESSION['cart'] 数组将替换为新值,那么它非常有用,但我希望它保存所有选定产品的详细信息,我该如何达到那个目的?

I am building a simple shop cart and I am getting the following array from product page(when the user submits add to cart button)...

Array ( [product_id] => 1 [weight] => 2 [size] => 4 [type] => cafe [cost] => 6323.86 )

and after that I am saving it to session to showing later on cart...

$_SESSION['cart']= $data; //$_POST values into $data

it works great for if user adds only 1 product into cart, but if he adds another product the $_SESSION['cart'] array replaces with new value, but i want it save all selected product's details, how can i achieve that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坠似风落 2025-01-04 01:55:51

将另一个数据级别放入购物车:

$pid = $data['product_id'];
$_SESSION['cart'][$pid] = $data;

这将完成记录该商品已放入购物车的简单任务。您需要添加额外的逻辑来处理数量更新等。

然而,值得注意的是:如果“成本”字段是通过客户端往返的,请不要相信该值。对于某人来说,添加大量昂贵的商品并将价格设置为 0 将会非常简单。 Boom 会进入您的电子商务系统。

Put another data level into your cart:

$pid = $data['product_id'];
$_SESSION['cart'][$pid] = $data;

which would do the simple task of recording that this item's been put in the cart. You'd need to add extra logic for handling quantity updates and whatnot.

However, of major note: if that 'cost' field is being roundtripped through the client, DO NOT TRUST THE VALUE. it will be extraordinarily simple for someone to add massive quantities of expensive items and set the price to 0. Boom goes your ecommerce system.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文