Categories: How ToMagento 2

How to Override Page Layout Programmatically in Magento 2

Layouts play a major role in Magento to override existing core files without affecting a system. Magento supports various types of layouts like 1 column, 2 column-left, 2 column-right, 3 columns, etc. The structure of all pages is described in the layout file. Technically, a layout is an .xml file which contains declarations and different instructions. When it comes to building a page inside your store to give a touch of the personalized store environment, you need to change your layout file at the end. But What if you acquire dynamic layout programmatically?

Today, I’m again back with a small piece of code that will help you to easily override page layout programmatically in Magento 2. You can even set page layout of the controller action which resides on similar location like this.
Go to app/code/Vendor/Extension/Controller/Youraction/Action.php ans paste below code here.


Happy Overriding!
Click to rate this post!
[Total: 7 Average: 3.1]
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.🏏

View Comments

  • Hi Dhiren,
    I am new to magento
    i followed same steps as like u mentioned in this page :override-page-layout-programmatically-magento-2/

    like:
    public function execute()
    {
    if($this->getRequest()->getParam('order_id')){
    $customerId = $this->customerSession->getCustomerId();
    $order = $this->orderFactory->create()->load($this->getRequest()->getParam('order_id'));
    if ($order->getId()
    && $order->getCustomerId()
    && $order->getCustomerId() == $customerId){
    $this->registry->register('current_order', $order);
    }else{
    $resultRedirect = $this->redirectFactory->create();
    return $resultRedirect->setUrl($this->url->getUrl('*/*/history'));
    }

    }

    echo "1";
    $page = $this->resultPageFactory->create(false, ['isIsolated' => true]);
    echo "2";
    // YOUR LOGIC TO GET SELECTED LAYOUT
    $youradminlayout = 'sales_order_viewinvoice_jp';
    $page->getConfig()->setPageLayout($youradminlayout);
    echo "3";
    return $page;
    }

    upto last line it is printing but page is not coming in front end.
    please help me on this.
    thanks in advance

Recent Posts

How to Integrate ChatGPT with Laravel Application?

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

3 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…

5 days 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…

5 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

6 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

7 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 week ago