0

I have overwritten this template magento_catalog/templates/product/list.phtml in my own theme. I have the following code to show a random number.

$number = mt_rand(1000,9999);

I print this number below the name of the product (code extract)

<h2 class="item-name">
    <strong class="product name product-item-name-list">
        <a class="product-item-link-list" href="<?php /* @escapeNotVerified */ echo $_product->getProductUrl() ?>">
            <?php /* @escapeNotVerified */ echo $_helper->productAttribute($_product, $_product->getName(), 'name'); ?>
        </a>
    </strong>
</h2>
<span><?php echo $number; ?></span>

But when I enter the category if I refresh the page, it always shows me the same number. it does not refresh...

What should I do to refresh that part?

Regards

  • It will be cached with full page cache. – harri Jan 29 '18 at 19:04
  • And how can I make this number not stored in cache? – willy Jan 29 '18 at 19:49
1

You can check here how to disable cache for a concrete block

Assuming you don't want to exclude the full product list, then you'd define a custom child block so you can disable cache just for it. Take a look at http://devdocs.magento.com/guides/v2.1/frontend-dev-guide/layouts/layout-practice.html for a complete reference about playing with layouts. This is an excerpt from there:

OrangeCo needs to create a new block, say, header.links, in the header.panel container, to move the links there. As the links can be added to this list by different modules, it is better to add this block to the default.xml page configuration of the Magento_Theme module.

So the following extending layout is added in the Orange theme:

app/design/frontend/OrangeCo/orange/Magento_Theme/layout/default.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="header.panel">
            <block class="Magento\Framework\View\Element\Html\Links" name="header.links">
                <arguments>
                    <argument name="css_class" xsi:type="string">header links</argument>
                </arguments>
            </block>
        </referenceContainer>
    </body>
</page>

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.