Magento添加到购物车问题缓存新产品 [英] Magento add to cart issue on caching new products

查看:484
本文介绍了Magento添加到购物车问题缓存新产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在首页的新产品小部件中添加到购物车功能时遇到问题。通过点击添加到购物车它加载/结帐/购物车/页面,但我收到购物车是空的!我检查了JS问题,但我没有任何错误,也在系统日志。
在缓存管理中,如果我只禁用BLOCK_HTML缓存,它工作正常;如果我重新启用它我有问题



网站由siteground托管,实际上在cron我有:
每25分钟:php / home /ledsuper/public_html/cron.php> / dev / null
每30分钟一次:/ bin / sh /home/ledsuper/public_html/cron.sh



什么是导致问题?我该怎么办去尝试解决?
Thx

解决方案

这个问题似乎是Cache存储了New Products包含旧(不正确)表单键的添加到购物车链接的新产品



表单键是什么

这是一种防止 XSS 攻击的机制,其中恶意攻击者可以在您使用其他浏览器标签时为购物车添加内容,甚至可以为您完成订单。这取决于可预测的URL,因为网站将无法访问您的Magento订单等待的浏览器选项卡中的实际HTML内容。发送到Magento商店的所有内容都将提交您的Cookie,从而使用您的会话。通过向每个表单或在服务器上生成操作的每个链接添加唯一键,URL或表单内容变得不可预测。表单密钥存储在会话数据中,并在提交给服务器时生效。



问题

$

b
$ b

这是在新产品小部件中的Magento 1.8中实现的添加到购物车的方式将生成表单键错误,因为它将缓存新产品的列表与添加到购物车链接,其中将包含首次请求页面的用户的表单键。



为什么当您不使用缓存时此问题不会显示



生成块时,使用方法 $在 product / widget / new / content / new_grid.phtml 中创建添加到购物车链接在 code / core / Mage / Catalog / Block / Product / Abstract.php 中定义的网址。



建议解决方案



我可以想到3个选项


  1. 简单的一个是将小部件缓存生命周期设置为1,因此
    不会缓存它。 (确保在更改后刷新所有的缓存。)这样做的(大)缺点是
    每个页面加载将调用重新渲染块,通过
    加载采集。

  2. 仅为添加到购物车按钮创建子块,该子块未完全缓存。

  3. 通过编辑 product / widget / new / content / new_grid.phtml 更改链接,并替换添加到购物车按钮指向产品页面的href,因此,不是将产品添加到购物车,而是将用户重定向到产品页面,在此处添加产品。这可能是最简单的,如果您可以使用工作流程。


I’m having problem with add to cart function in new products widget in the homepage. By clicking on "Add to cart" it load /checkout/cart/ page but I receive "The cart is empty!". I checked for JS problems but I haven’t any error, also in system log. In cache management, if I disable only BLOCK_HTML cache it works fine; if I re-enable it I have the problem

The website is hosted by siteground, actually in cron I have: every 25 min.: php /home/ledsuper/public_html/cron.php > /dev/null every 30 min.: /bin/sh /home/ledsuper/public_html/cron.sh

What is causing the problem? how can I do to try solve? Thx

解决方案

This problem seems to be that the Cache is storing the "New Products" block, which contains the New Products with a "Add To Cart" Link which contains an old (incorrect) form key

What is the form key

This is a mechanism to protect from XSS attack, where a malicious attacker can add stuff to your cart while you're in a different browser tab or even complete an order for you. This relies on predictable URLs, because the site will not have access to the actual HTML content in the browser tab where you have your Magento order waiting. Everything sent to the Magento store will however submit your cookies and thus use your session. By adding a unique key to each form or to each link that generates an action on the server, the URL or form content becomes no longer predictable. The form key is stored in the session data and validated upon submission to the server. If they don't match - you get a form key error and the action is not completed.

The issue Here

Is that the way the add to cart it is implemented in Magento 1.8 in the New Product widget will generate form key errors, because it will cache a list of new products together with "add to cart" link, which will contain the form key of the user that first requested the page.

Why this issue doesn't show up when you're not using caching

When the block is generated, the add to cart link is created in product/widget/new/content/new_grid.phtml using the method $this->getAddToCartUrl which is defined in code/core/Mage/Catalog/Block/Product/Abstract.php which adds the form_key to the url.

Proposed Solutions

There are 3 options that I can think of

  1. The simple one is to set the widget cache lifetime to 1, so it doesn't cache it at all. (Make sure to refresh all the caches after this change.) The (big) drawback from doing this, is that for every page load there will a call to re-render the block, by loading the collection.
  2. Create a child block just for the add to cart button, which is not cached at all.
  3. Change the link by editing product/widget/new/content/new_grid.phtml, and replacing the add to cart button, with a href to the product page, so that instead of adding the product to the cart, you redirect the user to the product page, where they add the product. This is probably the simplest, if you are OK with the workflow.

这篇关于Magento添加到购物车问题缓存新产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆