Magento2 产品列表小部件缓存问题 - 价格未重新加载
-
29-09-2020 - |
题
我在 magento 2 商店首页的内容区域中插入了一个产品列表小部件。使用以下代码插入小部件:
{{widget type="Magento\CatalogWidget\Block\Product\ProductsList" products_count="12" template="product/widget/content/grid.phtml" conditions_encoded="a:2:[i:1;a:4:[s:4:`type`;s:50:`Magento|CatalogWidget|Model|Rule|Condition|Combine`;s:10:`aggregator`;s:3:`all`;s:5:`value`;s:1:`1`;s:9:`new_child`;s:0:``;]s:4:`1--1`;a:4:[s:4:`type`;s:50:`Magento|CatalogWidget|Model|Rule|Condition|Product`;s:9:`attribute`;s:12:`category_ids`;s:8:`operator`;s:2:`==`;s:5:`value`;s:2:`19`;]]"}}
将站点切换到生产模式时,更改货币时不会重新加载产品价格。价格重新加载可以在开发者模式下以及常规类别页面上使用。刷新后端的 BLOCK_HTML 缓存后会重新加载价格,但随后会缓存在首页视图中选择的货币。将小部件缓存 TTL 设置为 null 无效。
我还尝试使用以下代码通过布局 xml 添加小部件:
<referenceContainer name="content.bottom">
<block class="Magento\CatalogWidget\Block\Product\ProductsList" template="product/widget/content/grid2.phtml" cacheable="false"><!-- -->
<arguments>
<argument name="products_count" xsi:type="number">12</argument>
<argument name="conditions_encoded" xsi:type="string">a:2:[i:1;a:4:[s:4:`type`;s:50:`Magento|CatalogWidget|Model|Rule|Condition|Combine`;s:10:`aggregator`;s:3:`all`;s:5:`value`;s:1:`1`;s:9:`new_child`;s:0:``;]s:4:`1--1`;a:4:[s:4:`type`;s:50:`Magento|CatalogWidget|Model|Rule|Condition|Product`;s:9:`attribute`;s:12:`category_ids`;s:8:`operator`;s:2:`==`;s:5:`value`;s:2:`19`;]]</argument>
</arguments>
</block>
</referenceContainer>
我也尝试添加 $this->_isScopePrivate = true;
到 Magento\CatalogWidget\Block\Product\ProductsList::__construct()
这应该强制 Ajax 加载,但没有成功。
有人建议如何解决这个问题吗?
解决方案
弄清楚了:添加 xsi:type null 的参数 cache_lifetime 就可以了。因此,如果您想通过layout.xml 添加小部件并且希望它独立于缓存,请执行以下操作:
<referenceContainer name="content.bottom">
<block class="Magento\CatalogWidget\Block\Product\ProductsList" template="product/widget/content/grid2.phtml">
<arguments>
<argument name="products_count" xsi:type="number">12</argument>
<argument name="conditions_encoded" xsi:type="string">a:2:[i:1;a:4:[s:4:`type`;s:50:`Magento|CatalogWidget|Model|Rule|Condition|Combine`;s:10:`aggregator`;s:3:`all`;s:5:`value`;s:1:`1`;s:9:`new_child`;s:0:``;]s:4:`1--1`;a:4:[s:4:`type`;s:50:`Magento|CatalogWidget|Model|Rule|Condition|Product`;s:9:`attribute`;s:12:`category_ids`;s:8:`operator`;s:2:`==`;s:5:`value`;s:2:`19`;]]</argument>
<argument name="cache_lifetime" xsi:nil="true" xsi:type="null">null</argument>
</arguments>
</block>
</referenceContainer>
其他提示
不确定这是否是自提出此问题以来添加的功能,但当您以管理员身份创建小部件时,参数中有一个“缓存生命周期(秒)”。
如果您有多种货币,这似乎需要设置为 0,因为它默认持续一天。
该小部件不包含用于决定一天缓存或无缓存是否是明智的默认设置的代码,这似乎很愚蠢 - 它只需要计算所使用的货币即可找到答案。
您也可以在调用小部件的块中尝试cache_lifetime =“1”。像下面这样的东西,
{{widget type="Magento\Catalog\Block\Product\Widget\NewWidget" cache_lifetime="1" display_type="all_products" products_count="8" template="Magento_Catalog::product/widget/new/content/new_grid.phtml"}}