0

Shall we load separate product detail page based on the product attribute value in magento2 ?

I have created the attribute product called "type_of_product"

If this attribute value is "Test 2", I need to load custom template file in product detail page.(A separate phtml file load all product data)

If any other value for attribute, Default magento product page should load.

Note: Once the value of attribute is "Test 2" I don't want to show any other default blocks of product detail page like image,price,tabs,links, etc..

How this can be implemented in M2.

Can anyone help me please.

2

Follow Below steps to load separate product detail page based on the product attribute value in magento2

Step 1 Create event and add below code to app/code/Vendor/ModuleName/etc/frontend/events.xml file

<?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\ModuleName\Observer\ProductViewLayout" />
    </event>
</config>

Step 2 Create Observer file and Add Below code to app/code/Vendor/ModuleName/Observer/ProductViewLayout.php

namespace Vendor\ModuleName\Observer;

use Magento\Framework\Event\ObserverInterface;


class ProductViewLayout implements ObserverInterface
{
    protected $_resultPageFactory;

    public function __construct(\Magento\Framework\View\Result\PageFactory $resultPageFactory) {
        $this->_resultPageFactory = $resultPageFactory;
    }
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $_product = $objectManager->create('Magento\Catalog\Model\Product')->load($observer->getProduct()->getId());
        $attributeValue = $_product->getResource()->getAttribute('type_of_product')->getFrontend()->getValue($_product);
        if($attributeValue=="Test 2"){
            $page = $this->_resultPageFactory->create();
            $page->addHandle('catalog_product_custom_layout');
        }
        return $this;
    }
}

Step 3 Create your custom layout to app/code/Vendor/ModuleName/view/frontend/layout/catalog_product_custom_layout.xml file and change code as per your requirement on this file

<?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.review" remove="true" />
        <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_ModuleName::product/view/custom.phtml" />
        </referenceContainer>
    </body>
</page>

You can call your custom template file using custom 'catalog_product_custom_layout.xml' file

You can change condition if($attributeValue=="Test 2"){ as per your attribute value in this ProductViewLayout.php file

15
  • will it remove all other blocks of detail page if attribute value is "test2"? like image and other blocks in view page? Jan 29 '19 at 9:17
  • It will remove all block and container call on catalog_product_view.xml. Because it will call custom layout 'catalog_product_custom_layout.xml' file and you can manage this custom layout file as per your requirements. Means you will want image block then copy image code from catalog_product_view.xml and paste it in layout 'catalog_product_custom_layout' file and also call your custom template file on custom layout file Jan 29 '19 at 9:22
  • @jafarpinjar Thanks to apply my solution. i am happy to help you Jan 29 '19 at 9:39
  • hi @saneer, in some cases it shows both the page, Jan 29 '19 at 10:01
  • Add another condition to ProductViewLayout.php file and call the second custom layout file on this php file and 'catalog_product_view.xml' and 'custom' layout file merge into the 'secondcustom' layout file Jan 29 '19 at 10:07
0

Hope all is well. I know your origional post is quite old but there does not seems much else available to achieve this.

In Magento 2.3.7 this is the error recieved

Got error 'PHP message: PHP Fatal error: Uncaught Error: Call to a member function getFrontend() on bool

in your ProductViewLayout around line 18 - would you be able to provide a solution for this - please

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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