Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
kandrejevs opened this issue Mar 24, 2016 · 36 comments
Closed

Comments

@kandrejevs
Copy link

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.

@kandrejevs kandrejevs changed the title Recently viwed products not working in product page with enabled cache Recently viewed products block not working in product page with enabled cache Mar 29, 2016
@gavinlimely
Copy link

Is there any solution to this?

@yosefkurniawan
Copy link

any solution on this?

@pboisvert
Copy link

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

@BenSpace48
Copy link
Contributor

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

@BenSpace48
Copy link
Contributor

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

@vkorotun vkorotun removed the MX label Aug 4, 2016
@veloraven
Copy link
Contributor

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

@bpgkt
Copy link

bpgkt commented Aug 17, 2016

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
Copy link

moloughlin commented Aug 26, 2016

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.

@tkacheva
Copy link

tkacheva commented Oct 20, 2016

Internal issue MAGETWO-56062

@dhduc
Copy link

dhduc commented Feb 16, 2017

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
Copy link
Contributor

BenSpace48 commented Feb 20, 2017

@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
Copy link

dhduc commented Feb 20, 2017

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

@BenSpace48
Copy link
Contributor

BenSpace48 commented Feb 20, 2017

@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
Copy link

dhduc commented Feb 20, 2017

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

@dan-advantec
Copy link

Unfortunately, this still appears to be an issue on 2.2.2

@magento-engcom-team magento-engcom-team added the Issue: Format is not valid Gate 1 Failed. Automatic verification of issue format is failed label Jan 13, 2018
@crantron
Copy link

crantron commented May 31, 2018

Still in 2.2.3.

@BenSpace48
Copy link
Contributor

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
Copy link
Contributor

BenSpace48 commented Jun 28, 2018

@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
Copy link

anupamyx commented Jan 14, 2019

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

pdohogne-magento pushed a commit that referenced this issue Mar 26, 2019
@anupamyx
Copy link

anupamyx commented Jun 10, 2019

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
Copy link

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
Copy link

@sumeetmobiwebtech Did you check my above comment ?#3890 (comment)

@sumeetmobiwebtech
Copy link

@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
Copy link

anupamyx commented Jul 12, 2019

Only use this #3890 (comment) . 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
Copy link

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

@peterjaap peterjaap reopened this Apr 9, 2020
@ghost ghost removed the Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development label Apr 9, 2020
@peterjaap
Copy link
Contributor

Issue still exists on 2.3.4.

@peterjaap
Copy link
Contributor

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
Copy link
Contributor

Bartlomiejsz commented May 5, 2020

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
Copy link
Contributor

konarshankar07 commented Oct 13, 2020

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
Copy link
Contributor

sidolov commented Oct 13, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

No branches or pull requests