Magento 2 – Product List Widget Cache Problem with Prices Not Reloading

block-cachefrontendmagento2widget

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?

Best Answer

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