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!!