Magento Tutorials

How to Extend a Layout in Magento 2

Hello Magento Friends,

In this Magento Tutorial, I will explain How to Extend a Layout in Magento 2.

The layout represents the structure of a webpage. It is advisable not to change the Magento module and theme layouts, for stability and securing your customizations so that they do not get deleted during the upgrade. To make changes you can extend a layout file in your custom theme of Magento 2. Instead of copying the extensive page layout or page configuration code than making the changes, extending a layout file is a great option.

Let’s learn the steps to Extend a Layout in Magento 2.

Steps to  Extend a Layout in Magento 2:

Step 1: Create theme extending file 

app/design/frontend/Themes/Yourtheme/Magento_Catalog/layout/catalog_product_view.xml

to 

vendor/magento/module-catalog/view/frontend/layout/catalog_product_view.xml

Example :

<?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>
        <move element="product.info.stock.sku" destination="product.info.price" after="product.price.final"/>
    </body>
</page>

Step 2: Processing extending layouts

Magento merges layout files as follows:

For each layout file in the list:

Loads layout handle declaration and layout instructions.

Appends to the result in the following format:

<?xml version="1.0"?>
<layouts xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <handle id="checkout_cart_index" label="Shopping Cart" type="page" parent="default">
        <!-- Layout instructions from checkout_cart_index.xml -->
    </handle>
    <handle id="checkout_onepage_index" label="One Page Checkout" type="page" parent="default">
        <!-- Layout instructions from checkout_onepage_index.xml -->
    </handle>
    <!-- ... -->
</layouts>

Where a handle ID is defined by the name of the corresponding layout file, and handle attributes are defined by the attributes of the root layout node of this layout file.

Conclusion:

This way you can easily Extend a Layout in Magento 2. If you have any questions, feel free to write in the comment below. Also, ensure that you share the article with your Magento friends. Stay connected for more Magento Tutorials by MageComp.

Happy Reading!

Click to rate this post!
[Total: 4 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

Five Essential Payroll Compliance Tips for eCommerce Startups

Are you setting up a payroll system for your eCommerce startup? Ensuring compliance with myriad…

15 hours ago

Optimizing Laravel Blade: Unlocking Advanced Fetcher Techniques

In the expansive universe of Laravel development, Blade serves as the stellar templating engine, propelling…

16 hours ago

Magento 2: Add Quantity Increment and Decrement on Category Page

Hello Magento Friends, In this blog, we will discuss about adding quantity increment and decrement…

3 days ago

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

6 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

1 week ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

1 week ago