0

I am loading a new template for product detail page based on the product attribute value.

I have used the following code

Vendor/Module/etc/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
   <event name="catalog_controller_product_view">
       <observer name="custom_product_view_layout" instance="Vendor\Module\Observer\ProductViewLayout" />
   </event>
</config>

Then inside Observer file

Vendor/Module/Observer/ProductViewLayout.php

class ProductViewLayout implements ObserverInterface
{
 protected $_resultPageFactory;
 protected $product;

 public function __construct(
  \Magento\Framework\View\Result\PageFactory $resultPageFactory,
  \Magento\Catalog\Model\Product $product
 ) {
    $this->_resultPageFactory = $resultPageFactory;
    $this->product = $product;
}
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
    $_product = $this->product->load($observer->getProduct()->getId()); 
    $attributeCode = $_product->getResource()->getAttribute('is_custom');
    if($attributeCode){
        $attributeValue = $attributeCode->getFrontend()->getValue($_product);       
        if(isset($attributeValue) && $attributeValue == 'Yes'){
            $page = $this->_resultPageFactory->create();
            $page->addHandle('catalog_product_custom_layout');
        }
         return $this;
      }
    }
 }

Then Vendor/Module/view/frontend/layout/catalog_product_custom_layout.xml

 <?xml version="1.0"?>
 <page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
  <body>
    <referenceBlock name="product.info.stock.sku" remove="true" />
    <referenceBlock name="product.price.final" remove="true" />
    <referenceBlock name="product.info.price" remove="true" />
    <referenceBlock name="reviews.tab" remove="true" />
    <referenceBlock name="product.info.addtocart" remove="true" />
    <referenceBlock name="product.info.addto" remove="true" />
    <referenceBlock name="product.info.mailto" remove="true" />
    <referenceBlock name="product.info.details" remove="true" />
    <referenceContainer name="content.aside" remove="true" />
    <referenceContainer name="product.info.main" remove="true" />
    <referenceContainer name="product.info.media" remove="true" />
    <referenceContainer name="content">
        <block class="Magento\Catalog\Block\Product\View" name="custom.layout.details" template="Vendor_Module::product/view/custom.phtml" />
    </referenceContainer>
  </body>
</page>

In my custom theme I have used below code to display recently viewed products in the tabs.

app/design/frontend/Vendor/Theme/Magento_Catalog/layout/catalog_product_view.xml

<referenceContainer name="content">
    <block class="Magento\Reports\Block\Product\Widget\Viewed" cacheable="false" name="product.info.viewed" template="Magento_Reports::widget/viewed/content/viewed_grid.phtml" group="detailed_info" >
        <arguments>
            <argument translate="true" name="title" xsi:type="string">Recently Viewed</argument>
            <argument name="page_size" xsi:type="number">4</argument>
            <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>
        </arguments>
    </block>
</referenceContainer>

So in the new template file we have custom html code to show this page is different than normal magento product page,

Before the footer we can see the product tabs which have Details and Review also Recently Viewed Tabs, once clicked Recently Viewed tab, the recently viewed products are not appearing.

This may be because we are not viewed the product page, so the recently viewed collection is empty.

Can anyone help me with this issue please!!

6
  • Default Magento 2 PDP showing Related and Upsell product sections. Are you calling this Recent view product section with Widget or code?. Can you please explain? Dec 24, 2020 at 11:32
  • @SaneerLadani, yes through code i am calling recently viewed products, in catalog_product_view.xml i have added the code for it Dec 24, 2020 at 11:58
  • Can you please explain the Recently view product code in detail or step by step?. I have checked your Question. but there is no any code related to the Recent view product. Dec 24, 2020 at 13:36
  • @SaneerLadani, I have updated my question, please have a look Dec 24, 2020 at 13:43
  • There is an Admin setting that will show the Recently Viewed Products for Not Logged in Customers. Go to Admin => Stores > Configuration > Catalog > Recently Viewed/Compared Products select Yes to (Synchronize widget products with backend storage) Dec 25, 2020 at 6:08

1 Answer 1

0

Please remove the below line from this Vendor/Module/view/frontend/layout/catalog_product_custom_layout.xml file

<referenceBlock name="product.info.details" remove="true" />

Please try with the Below code to this catalog_product_view.xml file

<referenceBlock name="product.info.details">
    <block class="Magento\Catalog\Block\Widget\RecentlyViewed" cacheable="false" group="detailed_info" name="product.info.recently.viewed" template="product/widget/viewed/grid.phtml">
        <arguments>
            <argument translate="true" name="title" xsi:type="string">Recently Viewed</argument>
            <argument name="page_size" xsi:type="number">4</argument>
            <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>
        </arguments>
    </block>
</referenceBlock>

Comment or remove your recent view XML code from catalog_product_view.xml and Please try with my(above) code and Clear your Magento cache.

3
  • hello @saneer,tried above solution, again same issue, the products which i am loading separate layout are not listing in recently viewed items Dec 26, 2020 at 13:51
  • hi @saneer, The product which we are loading separate layout based on attribute is not showing in recently viewed tabs. Dec 30, 2020 at 8:20
  • hello @saneer, have you cleared with it? Dec 31, 2020 at 9:07

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.