php Magento 如何保存您的购物车?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8419035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does Magento save your cart?
提问by Greg Demetrick
Magento has two ways to store a cart. Logged In users can have cart saving defined for as long as you want to define it and it is stored in the database tied to the user number. Non-logged in users seem to be bound by how long your site keeps it's session variables. This leads me to 2 questions.
Magento 有两种存储购物车的方法。登录用户可以定义购物车保存,只要您想定义它,它就会存储在与用户编号相关的数据库中。未登录的用户似乎受您的站点保留其会话变量的时间限制。这让我有两个问题。
1) Am I correct in thinking that non-logged in users carts are tied to session timeouts?
1)我认为未登录的用户购物车与会话超时有关,这是否正确?
2) Since Magento/Varien recommends fairly short times for killing session variables (usually only 4 hours), if question one is true, is there a way to keep a non-logged in cart around without changing the session time out variable?
2)由于 Magento/Varien 建议在相当短的时间内杀死会话变量(通常只有 4 小时),如果第一个问题是真的,有没有办法在不更改会话超时变量的情况下保持未登录的购物车?
回答by clockworkgeek
As I understand it carts are saved as quotes, even for guests. Logged in users have a customer ID which is stored with the quote, guests do not so their quote has a null customer ID, hence you may find a store has a lot of orphaned/incomplete quotes in the DB. The only way to associate a guest with their cart is by storing the quote ID in their session.
据我了解,推车被保存为报价,即使对于客人也是如此。登录用户有一个与报价一起存储的客户 ID,客人没有,所以他们的报价有一个空客户 ID,因此您可能会发现商店在数据库中有很多孤立/不完整的报价。将客人与其购物车相关联的唯一方法是在他们的会话中存储报价 ID。
You could extend how long their quote is available to them by storing the quote ID directly in their cookie with a long timeout but this leads to an obvious security breach; anyone could adjust the value in their cookie and view anyone else's cart.
您可以通过将报价 ID 直接存储在他们的 cookie 中并长时间超时来延长报价对他们可用的时间,但这会导致明显的安全漏洞;任何人都可以调整他们 cookie 中的值并查看其他人的购物车。
The only safe way is to proceed is to create a table of guest tokens and associate it with quotes (sorry no code this time, there's too much to explain in a low level). The token is the only public part and is set in the cookie. Tokens should be random and long, say 512-bits/64-chars, but not too long because they are included in every HTTP header. Every time a new session is created it might be a returning guest so check for a token and look it up in the table. Take the found quote ID and store that in the session thereby resurrecting the old cart. Quotes with customer IDs don't need to be rescued this way so should be exempt, especially since a logging-out customer doesn't want to see any part of their account remain visible.
唯一安全的方法是继续创建一个访客令牌表并将其与引号相关联(抱歉这次没有代码,在低级别解释太多)。令牌是唯一的公共部分,并在 cookie 中设置。令牌应该是随机且长的,比如 512 位/64 字符,但不能太长,因为它们包含在每个 HTTP 标头中。每次创建新会话时,它可能是一个返回的客人,因此请检查令牌并在表中查找。获取找到的报价 ID 并将其存储在会话中,从而恢复旧购物车。带有客户 ID 的报价不需要以这种方式拯救,因此应该被豁免,特别是因为注销的客户不希望看到他们帐户的任何部分仍然可见。
回答by boti
Take a look into your magento database at the table "sales_flat_quote"
在“sales_flat_quote”表中查看您的 magento 数据库
Regards boti
问候博蒂
回答by Judder
Carts are saved to the 'sales_flat_quote' table
购物车保存到“sales_flat_quote”表
The items in the basket are saved to 'sales_flat_quote_item', linked by the quote's entity_id
购物篮中的商品保存到“sales_flat_quote_item”,由报价的 entity_id 链接
Finally the options of the items are saved to 'sales_flat_quote_item_option' linked by the item_id above
最后,项目的选项被保存到由上面的 item_id 链接的 'sales_flat_quote_item_option'
Therefore to view all items and options for a saved quote
因此,查看已保存报价的所有项目和选项
select sfqi.item_id, sfqio.code, sfqio.value from sales_flat_quote AS sfq, sales_flat_quote_item AS sfqi, sales_flat_quote_item_option AS sfqio where sfqi.item_id = sfqio.item_id AND sfqi.quote_id = sfq.entity_id AND sfq.entity_id = '133940';