Magento2 – Update Layout Using Observer on Product Page

event-observerlayoutmagento2product-pagetheme

I want to Update XML using observer on Product Page when page load.
I want to set there condition with product Attribute there.
How can I do that?

Best Answer

What you can do is add layout via event layout_load_before, you can use this event to add your dynamic layout.

Here is sample code for you.

step: 1 create events.xml in your module

[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">
        <!-- for diffrentiate layout.xml on product basis -->
        <event name="layout_load_before">
            <observer name="load_custom_handler" instance="[Vendor]\[Module]\Observer\LayoutLoadBefore" />
        </event>
    </config>

Step:2 Now create observer LayoutLoadBefore.php

In your [Vendor][Module]\Observer\LayoutLoadBefore.php file write below code

    <?php

    namespace [Vendor]\[Module]\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');

            if (!$product){
              return $this;
            }
            if($product->getSku() =='product_sku'){ // add your multiple attribute condition
               $layout = $observer->getLayout();
               $layout->getUpdate()->addHandle('catalog_product_view_customlayout'); 
// here you will have to set custom layout which is for specific layout.
            }

            return $this;
        }
    }

Step 3: Create layout file for custom layout.

Create layout file in your custom theme/ custom module.

[Theme]/frontend/Magento_catalog/layout/catalog_product_view_customlayout.xml

or

[Vendor]/[Module]/view/frontend/layout/catalog_product_view_customlayout.xml

and write your code.

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        ...
            //write your custm code here.
        ...
    </body>
</page>

This file only rendered in your specific condition of products.

now in this file you can add your special requirements.

Hope this will help you !..

Related Topic