更新购物车篮 - ajax 还是不使用 ajax?
假设您有一个项目列表,然后单击“添加”按钮。项目名称将添加到购物篮框中。
这样做的最佳实践是什么?
1)当您点击添加按钮时,商品数据将通过ajax添加到购物车会话中,然后也通过ajax重新显示购物车框。
2)与1相同,但添加到数据库cart_temp表而不是购物车会话?
3) 如果没有 Ajax,当您单击“添加”按钮时,将通过 JavaScript 在购物篮框中创建新元素/标签。将添加商品名称和价格。在篮子框的底部,会有一个“继续”按钮。单击“继续”按钮会将购物车元素/标签中的数据放入会话或数据库中。如何使用jquery获取元素items和prices值?
4)或者你的解决方案是什么?
Let say you you have a list of items and you click on the Add button. The Item name will be added to the basket box.
What is the best practice doing this?
1) When you click on the add button, item data will be added to the cart session via ajax and then redisplay the basket box via ajax as well.
2) Same as number 1 but adding to DB cart_temp table instead of cart session?
3) Without Ajax, when you click on the Add button, new element/tag will be created on the basket box via JavaScript. Item name and price will be added. At the bottom of Basket Box, there will be a "Proceed" button. Click on the Proceed button will then put data from the cart elements/tag into session or database. How can use jquery to get the elements items and prices value?
4) Or what your solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于与购物车管理相关的所有问题,没有通用的解决方案。对于任何地方的每个人来说都是通用的解决方案。但有一些经过测试且运行良好的设计模式。此外,我还可以知道我的软件正在使用哪种型号。
首先您必须知道,每次价格修改、折扣、促销代码都可以修改原始产品价格。使用 JavaScript 计算可能不太稳定且不可靠。
应该有一个/几个可信方法来返回分配给购物车的信息,例如
方法将返回固定值,例如附加税或其他值。其次,您需要使用或不使用ajax从JavaScript管理它。我更喜欢以 JSON 格式获取所有数据。
看看: http://api.jquery.com/jQuery.getJSON/
当你有获取购物车总价的一种方法很简单,可以在您的网站上的任何地方获取它。当有一个像类这样的中心位置时,引入像促销代码这样的新功能会不那么痛苦。想象一下,您的网站上只有很少的地方是通过 JavaScript 计算价格的,编辑时很容易错误地破坏某些内容。
http://yuml.me/8317d38
此模型在具有 API 架构等结构实现的网站上很常见。
也许它会对你有所帮助,祝你好运。
There is no general solution for all kind of problems connected with cart management. Solutions that will be universal for everyone, everywhere. But there are tested and well working design patterns. Furthermore I can tell which model I am using with my software.
First of all You have to know that each price modification, discount, promo code can modify original product price. Calculating it using JavaScript can be less stable and unreliable.
There should be one/few trusted method to return information assigned to a cart, for e.g.
Methods will return solid values e.g. with added tax or something. Secondly You need to manage it from JavaScript using ajax or not. I prefer fetching all data in JSON format.
Look at: http://api.jquery.com/jQuery.getJSON/
When You have one method to get cart total price it is simple to fetch it everywhere on Your website. Introducing new functionality like promo codes will be less painful when there is a one central place like a Class. Imagine that You have few places on your website where price is calculated by JavaScript, it is simple to break something by mistake when editing.
http://yuml.me/8317d38
This model is common on websites with structure implemented like an API Schema.
Maybe it will help You, good luck.
我认为 AJAX 调用越少越好。我选择了第四种解决方案(#4)。基本上,在项目单击处,您可以(通过 jQuery)添加一些 HTML 隐藏字段,例如:
然后在创建“继续”按钮后处理所有内容。
至少我会这么做。
回答这个:
您应该从价格字段中取出它并添加它以显示当前价格。但我会避免将价格存储在 HTML 页面或 Javascript 中:最好让 PHP 脚本计算发送产品列表的实际价格。
I think that the less AJAX calls you make, the better it is. I'd opted for the fourth solution (#4). Basically at the item click you could (trough jQuery) add few HTML hidden fields such as:
And then process everything after creating a "Proceed" button.
That's what, at least, I'd do.
Answering this:
You should just take it from your price field and add it show it's current price. But I'd avoid the storing of the price in the HTML page or on Javascript: it's better to make your PHP script calculate the real price sending the list of the products.
最好的选择是通过 AJAX 往返服务器并发回购物篮的 HTML。 HTML 是如此之小,以至于 AJAX 开销无论如何都会花费更多时间,但您确实需要使用服务器端逻辑来获得正确的定价。
如果降级对于非 JavaScript 很重要,请刷新整个页面。
The best bet is to make a round trip to the server, via AJAX and send back the HTML for the basket. The HTML is so small that the AJAX overhead will take way more time anyway, but you really need to use the server-side logic to get the pricing right.
If degrading is important for non-javascript, do a whole page refresh.