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 Add Tooltip in Checkout Shipping Field in Magento 2?

Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…

3 days ago

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

4 days ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

5 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

7 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

1 week ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

1 week ago