Magento2: Recently viewed products block not working in product page with enabled cache

Created on 24 Mar 2016  路  37Comments  路  Source: magento/magento2

Steps to reproduce

  1. add:
    <block class="Magento\Reports\Block\Product\Widget\Viewed" page_size="4" name="recently_viewed" template="widget/viewed/content/viewed_grid.phtml" cacheable="false" /> to catalog_product_view.xml in any container

Expected result:
With enabled cache you should see recently viewed products block in product page.

Actual result:
Block is visible only with disabled cache, when cache is enabled, it does not have output.

Catalog FrameworCache Format is not valid done bug report

Most helpful comment

Hi @slavvka,

Please keep the 2.1.x issues open until the fix is backported to 2.1-develop
If there isn't a backport scheduled via an internal MAGETWO task, please mark the issue with an up for grabs label so that anyone from the community was able to notice it and make a backport PR voluntarily.

All 37 comments

Is there any solution to this?

any solution on this?

@choukalos can you have Oleh check into this if it may be caching related?

I am having the same problem, any update on this Magento?

@pboisvert @choukalos - Any update please? This has been going on for a while (sorry for asking again but this is proving to be pretty problematic).

@kandrejevs , @BenSpace48
Please, provide the used Magento version. Was it clear install or upgrade from some other version?

I do have 2.1 and the issue is still not resolved.
(i use varnish too)

btw. disabling cache, but keeping varnish on = blocks will show up;

I was noticing similar issues where my private_content_version cookie (used for the AJAX fetching of private content) wasn't set, and requesting the page didn't recreate it for me. Interestingly, adding an item to cart recreates the cookie and I'm able to see these blocks again.

In \Magento\Framework\App\PageCache\Version::process, you can see that the private_content_version cookie isn't actually set until the first POST request is made. I suspect this is probably a bug, since sessions can still need to load private content without this precondition being met.

Steps I've used to produce:

  • Add the built-in recently viewed widget to some page (I'm using product and category pages)
  • Create a new private session
  • Load a product page
  • Navigate to a page where the widget is expected to appear

Expected result: widget appears
Actual result: no such luck

Now, add a product to the cart and navigate back to the page where the widget appears and it will render as expected.

Internal issue MAGETWO-56062

The simple solution I've used is disable caching of the Recently Viewed block by PageCache module.
I just set cacheable="false" for the block as below:

<block class="Magento\Reports\Block\Product\Widget\Viewed" cacheable="false" name="product.info.viewed" template="Magento_Reports::widget/viewed/content/viewed_grid.phtml" after="product.info.upsell" >
    <arguments>
        <argument name="page_size" xsi:type="number">9999</argument>
    </arguments>
</block>

Update: It's just working for me, but it's a terrible solution, see below comment of @BenSpace48.

@dhduc Doesn't that stop the whole page from being cached though? I'm sure I've heard that a few times, and if so that is a terrible workaround as the side effect is potentially worse than the initial issue.

Edit: It does stop the whole page from being cached:

To create an uncacheable page, mark any block on that page as uncacheable in the layout using cacheable="false".

Source - http://devdocs.magento.com/guides/v2.1/config-guide/cache/cache-priv-over.html

@BenSpace48 Yes, you're right. It's my fault, so my teammate suggests me to using AJAX alternative. Is it a better solution?

@dhduc I only know about that because I did the exact same workaround 馃槺

Yeah I'd say so, I think hole punching that block so it isn't cached but the rest is is the best solution but that is more back-end which is not my speciality so take that with a pinch of salt.

@BenSpace48 (y) Thanks for letting me know about my solutions.

I resolved with _bin/magento cache:clean_ after enabling cache. For me it worked.
I'm using Magento 2.1.3 and I've only tested in _developer mode_.

@DonnieDi - Wouldn't the problem just come back once the block is cached though?

The issue has been fixed for 2.2

Hi @slavvka,

Please keep the 2.1.x issues open until the fix is backported to 2.1-develop
If there isn't a backport scheduled via an internal MAGETWO task, please mark the issue with an up for grabs label so that anyone from the community was able to notice it and make a backport PR voluntarily.

Internal ticket to track issue progress: MAGETWO-66615

what is wrong with using:

` /**

  • Copyright 漏 2013-2017 Magento, Inc. All rights reserved.
  • See COPYING.txt for license details.
    */

namespace Magento\Reports\Block\Product;

use \Magento\Framework\DataObject\IdentityInterface;

/**

  • Reports Recently Viewed Products Block
    *
  • @author Magento Core Team core@magentocommerce.com
    */
    class Viewed extends AbstractProduct implements IdentityInterface
    {
    protected function _construct()
    {
    $this->addData(array('cache_lifetime' => false));
    }
/**
 * Config path to recently viewed product count
 */
const XML_PATH_RECENTLY_VIEWED_COUNT = 'catalog/recently_products/viewed_count';

`

Unfortunately, this still appears to be an issue on 2.2.2

Still in 2.2.3.

I'm running into a strange issue where sometimes the recently viewed block shows and other times it does not, this is in Magento 2.2.3.

Where the block should show I'm seeing these tags:

screen shot 2018-06-28 at 09 27 08

The block was added with this XML:

<block class="Magento\Reports\Block\Product\Widget\Viewed" name="recently_viewed" template="widget/viewed/column/viewed_images_list.phtml" before="footer.social"/>

@dan-advantec and @crantron - Have you tried adding a product to bag?

For some weird reason recently viewed products will only load once a product has been added to bag, I can replicate this on two completely separate production websites.

Any update on this @magento-engcom-team? Should we create a new issue?

@BenSpace48 no I haven't, due to the inconsistencies we dropped this feature for our client. Which has lead us to abandon Magento 2 for future projects. Not only because of this, but MANY other issues with the platform. Maybe Adobe will rescue Magento 2 CE :smirk:

I'm running into a strange issue where sometimes the recently viewed block shows and other times it does not, this is in Magento 2.2.3.

Where the block should show I'm seeing these tags:

screen shot 2018-06-28 at 09 27 08

The block was added with this XML:

<block class="Magento\Reports\Block\Product\Widget\Viewed" name="recently_viewed" template="widget/viewed/column/viewed_images_list.phtml" before="footer.social"/>

Did u find a solution yet ? This behaviour is happening for me as well in 2.2.6

I fixed this via my custom script :

<script type="text/javascript">
        (function(){
            let sortable = [];
           //Fetch recently viewed products from local storage. Magento keeps it there.
            rvp = JSON.parse(localStorage.recently_viewed_product);
            var limit=0;
            for (let rv in rvp) {
                sortable.push(rvp[rv]);
                limit++;
                if(limit>7){
                    break;
                }
            }
            sortable.sort(function(o1,o2){return o2.added_at - o1.added_at;});
        }());
        //Now you can send this sortable array as an ajax to backend and fetch the product information accordingly.
    </script>

issue is still exists in magento 2.3.1
when cache is enable then recently products is not showing
Anyone know how to solve this ?

@sumeetmobiwebtech Did you check my above comment ?https://github.com/magento/magento2/issues/3890#issuecomment-500468475

@anupamyx
where i need to write that code ?
i am using that code -
myblock.php

 protected $recentlyViewed;
public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\Reports\Block\Product\Viewed $recentlyViewed,
        array $data = []
    )
    {        
        $this->recentlyViewed = $recentlyViewed;
        parent::__construct($context, $data);
    }

public function getRecentlyProducts()
    {
        $collection = $this->recentlyViewed->getItemsCollection()->load();
        return $collection->getData();
    }

my.phtml

$recentlyProducts = $this->getRecentlyProducts();
foreach ($recentlyProducts as $product) {
    echo $product['sku'];
    echo "<br>";
}

When cache is disable then this code working but when cache is enable then it is not working.
And when i write your above code in my.phtml then it will be work @anupamyx ?

Only use this https://github.com/magento/magento2/issues/3890#issuecomment-500468475 . Magento (2.3.0) puts recently viewed in browser's local storage. This uses that. Once you get the product ids you'll have to write a controller which sends you the product information from product ids

Sorry,i am not getting.
It will be great if you write step by step.

Issue still exists on 2.3.4.

Some more info; the \Magento\Reports\Block\Product\Widget\Viewed block ultimately extends the abstract block \Magento\Reports\Block\Product\AbstractProduct. This class contains the \Magento\Framework\View\Element\AbstractBlock::$_isScopePrivate property, which is set to true.

However, the Magento DevDocs on Private Content states;

Do not use the $_isScopePrivate property in your blocks. This property is obsolete and will not work properly.

This appears to be the case in this core code. So the recently viewed block code should be rewritten to not rely on $_isScopePrivate but instead use customer-data JS library along with a block, a template and a UI component to render the recently viewed block on a per-customer basis.

Shouldn't this be closed since there was catalog_recently_viewed widget introduced that uses Magento\Catalog\Block\Widget\RecentlyViewed which doesn't seem to have same issue?

I agree with @Bartlomiejsz . If anyone want to show the recently viewed product then please use the below code

<block class="Magento\Catalog\Block\Widget\RecentlyViewed"
               name="recently.viewed.product">
            <arguments>
                <argument name="uiComponent" xsi:type="string">widget_recently_viewed</argument>
                <argument name="show_attributes" xsi:type="string">name,image,price</argument>
                <argument name="show_buttons" xsi:type="string">add_to_cart</argument>
                <argument name="template" xsi:type="string">product/widget/viewed/grid.phtml</argument>
            </arguments>
        </block>

@sidolov Can we close this issue?
Thanks

Thanks @Bartlomiejsz , @konarshankar07 ! Closing the issue according to the comment above.

Was this page helpful?
0 / 5 - 0 ratings