0

I have added the /etc/widget.xml, to call the recently viewed in Footer container.

The recently viewed block is display but it's applied with cache so the newly viewed products are not updated.

The Recently viewed section is getting updated, Only after the cache is flushed.

<widgets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Widget:etc/widget.xsd">
     <widget id="catalog_recently_viewed" class="Magento\Catalog\Block\Widget\RecentlyViewed"
            placeholder_image="Magento_Catalog::images/product_widget_viewed.gif"> 
        <containers> 
             <container name="footer">
                <template name="grid" value="grid" />
                <template name="list" value="list" />
            </container> 
        </containers>
    </widget>
</widgets>

2 Answers 2

1

I resolved the issue by using widget and cms block concept. Since widget is working via the ajax request, that's a power of widget!.

Magento 2.2.7 Version

Step 1: Module file path : root/app/Code/Faceless/RecentlyViewed/Setup/UpgradeData.php

<?php

namespace Faceless\RecentlyViewed\Setup;

use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
class UpgradeData implements UpgradeDataInterface
{


    private $blockFactory;

    public function __construct(
        \Magento\Cms\Model\BlockFactory $blockFactory
    ) {
        $this->blockFactory = $blockFactory;
    }

    public function upgrade(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $setup->startSetup();
        $cmsblockModel = $this->blockFactory->create()->load(
            'recently_viewed', 'identifier'
        );
        $cmsblockModel->getId() ? $cmsblockModel->delete() : '';        
        if (version_compare($context->getVersion(), '1.0.1') < 0) {
            $cmsBlockData = [
                'title' => 'Recently viewed',
                'identifier' => 'recently_viewed',
                'content' => '{{widget type='
                . '"Magento\Catalog\Block\Widget\RecentlyViewed"'
                . ' uiComponent="widget_recently_viewed" page_size="5" '
                . 'show_attributes="name,image,price" show_buttons="add_to_cart"'
                . ' template="product/widget/viewed/grid.phtml"}}',
                'is_active' => 1,
                'stores' => [0, 1, 2],
                'sort_order' => 0
            ];
            $this->blockFactory->create()->setData($cmsBlockData)->save();
        }
        $setup->endSetup();
    }
}

Step 2: Module file path : root/app/Code/Faceless/RecentlyViewed/view/frontend/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="footer">
            <container name="footer.recent" as="footerRecent" htmlTag="div" htmlClass="recently-viewed-wrap" before="-">
                <block class="Magento\Cms\Block\Block" name="Recentlyviewed">
                    <arguments>
                        <argument name="block_id" xsi:type="string">recently_viewed</argument>
                    </arguments>
                </block>
            </container>
        </referenceContainer>
    </body>
</page>

Step 3: Module file path : root/app/Code/Faceless/RecentlyViewed/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Faceless_RecentlyViewed" setup_version="1.0.1" schema_version="1.0.1">
        <sequence>
            <module name="Magento_Catalog" />
        </sequence>
    </module>
</config> 

Step 4: Module file path : root/app/Code/Faceless/RecentlyViewed/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Faceless_RecentlyViewed',
    __DIR__
);
-1

You just need to set below configuration

Stores > Configuration > Catalog > Catalog > Recently Viewed/Compared Products > Lifetime of products in Recently Viewed Widget

enter image description here

5
  • but adding cacheable="false" will ignore page from cache right ?! Commented Sep 10, 2019 at 10:01
  • yes right @iAmRajaVarien
    – Devidas
    Commented Sep 10, 2019 at 10:04
  • I am afraid of cacheable="false", Magneto not recommending this. Please refer the devDoc devdocs.magento.com/guides/v2.2/extension-dev-guide/cache/…. Also widget is works based on UI components and it's trigger the ajax request. Commented Sep 10, 2019 at 10:14
  • Using cacheable="false" is a very bad idea. It will result in the entire page bypassing varnish. This means if you use the recently viewed block on say your product and category pages then your entire catalog would not be cached significantly increasing load on your server and slowing down page TTFB. Short answer, don't do this. Commented Nov 4, 2020 at 21:59
  • Noted......Thanks to @AndrewKett
    – Devidas
    Commented Nov 5, 2020 at 6:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.