Skip to main content
Tweeted twitter.com/StackMagento/status/1220723084493193216
added 2007 characters in body
Source Link
Ravi Soni
  • 1.8k
  • 1
  • 16
  • 34

I have Two product. Both are Downloadable. I want different layout for both product.

Except below concept:

<catalog_product_view_id_productid>catalog_product_view_id_1.xml

The second concept I have 1 custom themeam using is with help of observer, So I haveBut also tried to created new custom theme again. And I have selected second custom theme in product edit from Adminthis one is not working. But in this case I load this product on frontendBelow are my code. ButPlease see and let me know if any change.

Path: etc/event.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="layout_load_before">
    <observer name="catalog_product_view_extension" instance="Custom\Extensionlayout\Observer\LayoutLoadBefore" />
</event>
</config>

Path: Observer/LayoutLoadBefore.php

<?php

namespace Custom\Extensionlayout\Observer;

class LayoutLoadBefore implements \Magento\Framework\Event\ObserverInterface
{
/**
 * @var \Magento\Framework\Registry
 */
protected $_registry;

public function __construct(
   \Magento\Framework\Registry $registry
)
{
    $this->_registry = $registry;
}


public function execute(\Magento\Framework\Event\Observer $observer)
{
    $product = $this->_registry->registry('current_product');


    //echo ; die;

    if (!$product){
      return $this;
    }
    if($product->getName() == 'book2'){ // your condition

       
       $layout = $observer->getLayout();
       $layout->getUpdate()->addHandle('catalog_product_view_extension');
    }

    return $this;
}
}

And here alsois my First custom theme called instead of my Second theme which I selected from Admin Product editlayout path: view/frontend/layout/catalog_product_view_extension.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>
    <referenceBlock name="page.main.title" remove="true"/>
    <referenceBlock name="product.info.overview" remove="true"/>
    <referenceBlock name="product.price.final" remove="true"/>
    <referenceBlock name="product.price.tier" remove="true"/>
    <referenceBlock name="product.info.stock.sku" remove="true"/>
    <referenceBlock name="product.info.review" remove="true"/>
    <referenceBlock name="product.info.addtocart.additional" remove="true"/>
    </body>
    </page>

AnyoneHere what happens, Observer is getting called but my custom handler not rendered. Anyone did this kind of change. Please suggest.

I have Two product. Both are Downloadable. I want different layout for both product.

Except below concept:

<catalog_product_view_id_productid>

I have 1 custom theme, So I have also tried to created new custom theme again. And I have selected second custom theme in product edit from Admin. But in this case I load this product on frontend. But here also my First custom theme called instead of my Second theme which I selected from Admin Product edit.

Anyone did this kind of change. Please suggest.

I have Two product. Both are Downloadable. I want different layout for both product.

Except below concept:

catalog_product_view_id_1.xml

The second concept I am using is with help of observer, But also this one is not working. Below are my code. Please see and let me know if any change.

Path: etc/event.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="layout_load_before">
    <observer name="catalog_product_view_extension" instance="Custom\Extensionlayout\Observer\LayoutLoadBefore" />
</event>
</config>

Path: Observer/LayoutLoadBefore.php

<?php

namespace Custom\Extensionlayout\Observer;

class LayoutLoadBefore implements \Magento\Framework\Event\ObserverInterface
{
/**
 * @var \Magento\Framework\Registry
 */
protected $_registry;

public function __construct(
   \Magento\Framework\Registry $registry
)
{
    $this->_registry = $registry;
}


public function execute(\Magento\Framework\Event\Observer $observer)
{
    $product = $this->_registry->registry('current_product');


    //echo ; die;

    if (!$product){
      return $this;
    }
    if($product->getName() == 'book2'){ // your condition

       
       $layout = $observer->getLayout();
       $layout->getUpdate()->addHandle('catalog_product_view_extension');
    }

    return $this;
}
}

And here is my custom layout path: view/frontend/layout/catalog_product_view_extension.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>
    <referenceBlock name="page.main.title" remove="true"/>
    <referenceBlock name="product.info.overview" remove="true"/>
    <referenceBlock name="product.price.final" remove="true"/>
    <referenceBlock name="product.price.tier" remove="true"/>
    <referenceBlock name="product.info.stock.sku" remove="true"/>
    <referenceBlock name="product.info.review" remove="true"/>
    <referenceBlock name="product.info.addtocart.additional" remove="true"/>
    </body>
    </page>

Here what happens, Observer is getting called but my custom handler not rendered. Anyone did this kind of change. Please suggest.

Source Link
Ravi Soni
  • 1.8k
  • 1
  • 16
  • 34

Magento2: How to set product wise different layout

I have Two product. Both are Downloadable. I want different layout for both product.

Except below concept:

<catalog_product_view_id_productid>

I have 1 custom theme, So I have also tried to created new custom theme again. And I have selected second custom theme in product edit from Admin. But in this case I load this product on frontend. But here also my First custom theme called instead of my Second theme which I selected from Admin Product edit.

Anyone did this kind of change. Please suggest.