0

My website has 5 types of layout of page,in which 4 types layout of Configure Product and 1 type is Simple Product Page.

I am loading these layout by observer by checking the current category/product page and on the basis of that loading the layout file.

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

Everyting is working fine on product page.

Now when I am going to cart page and edit any item from cart page then it call layout file "checkout_cart_configure.xml" file and when open this file you will find code :

<update handle="catalog_product_view"/>
    <body>
        <referenceBlock name="head.components">
            <block class="Magento\Framework\View\Element\Js\Components" name="checkout_cart_configure_head_components" template="Magento_Checkout::js/components.phtml"/>
        </referenceBlock>
        <referenceBlock name="product.info">
            <block class="Magento\Checkout\Block\Cart\Item\Configure" name="checkout.cart.item.configure.block"/>
        </referenceBlock>
        <referenceBlock name="product.info.addtocart">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Magento_Checkout::cart/item/configure/updatecart.phtml</argument>
            </action>
        </referenceBlock>
        <referenceBlock name="product.info.addtocart.additional">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Magento_Checkout::cart/item/configure/updatecart.phtml</argument>
            </action>
        </referenceBlock>
    </body>

So my question is you can see the code "" so it called default catalog_product_view.xml file.But in my code there are 5 types xml files. e.g catalog_product_view_type_simple.xml , catalog_product_view_type_configurable.xml , catalog_product_view_id_123.xml .....etc.

So it is not called my above files in "checkout_cart_configure.xml" .Hence updatecart.phtml file is not called.

Please let me know in this situation how can handle this.

0

Finally I resolved this problem by calling different layout file "checkout_cart_configure.xml" for different product layout.

I do this with event observer layout_load_before .

<?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="custom_product_layout" instance="Vendorname\Modulename\Observer\LayoutLoadBefore" disabled="false"/>
    </event>
</config>

in LayoutLoadBefore.php file code:

<?php

namespace Vendorname\Modulename\Observer;

use Magento\Checkout\Model\Session as CheckoutSession;

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 ($observer['full_action_name'] == 'checkout_cart_configure') {

            if (!$product) {
                return $this;
            }

                if ($product->getName() == 'Test1') { 
                    $layout = $observer->getLayout();
                    $layout->getUpdate()->addHandle('checkout_cart_configure_645');
                } elseif ($product->getName() == 'Test2') { 
                    $layout = $observer->getLayout();
                    $layout->getUpdate()->addHandle('checkout_cart_configure_839');
                else {
                    $layout = $observer->getLayout();
                       $layout->getUpdate()->addHandle('checkout_cart_configure_configurable');
                }
            }
        }
   return $this;
    }

}

Now Create checkout_cart_configure files in Magento_Checkout/layout folder.

Now update handle in checkout_cart_configure.xml :

<update handle="catalog_product_view_simple_product"/> //Name as per your handle
    <body>
        <referenceBlock name="head.components">
            <block class="Magento\Framework\View\Element\Js\Components" name="checkout_cart_configure_head_components" template="Magento_Checkout::js/components.phtml"/>
        </referenceBlock>
        <referenceBlock name="product.info">
            <block class="Magento\Checkout\Block\Cart\Item\Configure" name="checkout.cart.item.configure.block"/>
        </referenceBlock>
        <referenceBlock name="product.info.addtocart">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Magento_Checkout::cart/item/configure/updatecart.phtml</argument>
            </action>
        </referenceBlock>
        <referenceBlock name="product.info.addtocart.additional">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Magento_Checkout::cart/item/configure/updatecart.phtml</argument>
            </action>
        </referenceBlock>
    </body>

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.