0

I have a product list widget inserted in the content area of a magento 2 store frontpage. The widget is inserted using the following code:

{{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`;]]"}}

When switching the site to production mode, product prices are not reloaded when changing currencies. Price reload does work in developer mode and also on the regular category pages. Prices are reloaded after refreshing BLOCK_HTML cache in backend but will then cache the currency chosen on first page view. setting the widgets cache TTL to null has no effect.

I also tried adding the widget via layout xml using this code:

<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>

I also tried adding $this->_isScopePrivate = true; to Magento\CatalogWidget\Block\Product\ProductsList::__construct() which is supposed to force Ajax loading, but no success.

Suggestions how to get around this, anyone?

2

Figured it out: Adding an argument cache_lifetime of xsi:type null did the trick. So if you ever want to add a widget via layout.xml and you want it to be cache-independent, do this:

<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>
| improve this answer | |
  • And you add that custom layout xml on the cms page/category instead of using a widget instance yeah? – Erfan Dec 23 '16 at 17:35
  • yes, you can use it on the layout updates section via the backend or any layout xml file you use in your theme or extension – mybinaryromance Jan 4 '17 at 9:10
  • Note that as of Magento 2.2, it is no longer allowed to set the argument to "null". – Andrew C. Stayart Jun 19 '18 at 16:02
  • does this even work with FPC ? I pretty sure that this should not work – Gosu Przmak Mar 16 at 10:57
  • I validate your solution and your solution does not work with FPC, so I would say this is not a valid solution, unless you are not using FPC, but please mark it with bold text then – Gosu Przmak Mar 16 at 11:11
2

Not sure if it's a feature that's been added since this question was asked, but there is a "Cache Lifetime (Seconds)" in the parameters when you create the widget as an admin.

If you have multiple currencies, this seems to require setting to 0 as it lasts a day by default.

Seems daft that the widget doesn't contain the code to decide if a day cache or no cache would be the sensible default setting - it would just need to count the currencies being used to find out.

| improve this answer | |
0

Also you can try cache_lifetime="1" in your block, where widget is called. Something like below,

{{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"}}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.