magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.56k stars 9.32k forks source link

Recently viewed products block not working in product page with enabled cache #3890

Closed kandrejevs closed 4 years ago

kandrejevs commented 8 years ago

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.

gavinlimely commented 8 years ago

Is there any solution to this?

yosefkurniawan commented 8 years ago

any solution on this?

pboisvert commented 8 years ago

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

BenSpace48 commented 8 years ago

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

BenSpace48 commented 8 years ago

@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).

veloraven commented 8 years ago

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

bpgkt commented 8 years ago

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;

moloughlin commented 8 years ago

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:

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.

tkacheva commented 8 years ago

Internal issue MAGETWO-56062

dhduc commented 7 years ago

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.

BenSpace48 commented 7 years ago

@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

dhduc commented 7 years ago

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

BenSpace48 commented 7 years ago

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

dhduc commented 7 years ago

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

andre-gra commented 7 years ago

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.

BenSpace48 commented 7 years ago

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

slavvka commented 7 years ago

The issue has been fixed for 2.2

korostii commented 7 years ago

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.

magento-team commented 7 years ago

Internal ticket to track issue progress: MAGETWO-66615

southerncomputer commented 7 years ago

what is wrong with using:

`<?php /**

namespace Magento\Reports\Block\Product;

use \Magento\Framework\DataObject\IdentityInterface;

/**

`

dan-advantec commented 6 years ago

Unfortunately, this still appears to be an issue on 2.2.2

crantron commented 6 years ago

Still in 2.2.3.

BenSpace48 commented 6 years ago

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"/>
BenSpace48 commented 6 years ago

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

anupamyx commented 5 years ago

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

anupamyx commented 5 years ago

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>
sumeetmobiwebtech commented 5 years ago

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 ?

anupamyx commented 5 years ago

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

sumeetmobiwebtech commented 5 years ago

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

anupamyx commented 5 years ago

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

sumeetmobiwebtech commented 5 years ago

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

peterjaap commented 4 years ago

Issue still exists on 2.3.4.

peterjaap commented 4 years ago

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.

Bartlomiejsz commented 4 years ago

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?

konarshankar07 commented 4 years ago

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

sidolov commented 4 years ago

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