Question

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?

Was it helpful?

Solution

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>

OTHER TIPS

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.

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"}}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top