How to Remove Block From Layout in Magento 2
Magento 2 store designs must constantly be updated based on the business requirements, latest trends, and for the improvement of customer experience.
As a part of this process, you may require to add or remove a block from the layout in Magento 2. Here, I’ve shown how to remove block from layout in Magento 2 with a programmatic solution.
One can use this method in various ways. For example, the store is running a Christmas offer and had added a promotional offer block on the home page which needs to be removed post offer. Also, you may want to remove any default blocks for which the method is useful.
I’ve taken an example of removing the coupon code application block on the checkout page. If you want to remove a promo code that you are currently offering, you can do so by removing that block from the checkout.
Steps to Remove Block From Layout in Magento 2:
All you have to do is:
1 |
<referenceBlock name="block_name" remove="true"/> |
For example, remove the coupon code block from the checkout cart page:
In checkout_cart_index.xml,
1 2 3 4 5 6 |
<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceBlock name="checkout.cart.coupon" remove="true"/> </body> </page> |
That’s it.
Any doubts in the implementation? Mention them in the Comments section below and I’d be glad to help 😊
I’d be very grateful if you share this helpful post on social media to fellow developers!
Thanks!
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
2 Comments
I tried this, but it doesn’t seem to work. I’m using Magento 2.2.10.
Hello,
Can you please share which block are you trying to remove and from which XML file? It would be easy to help you out if you share how you are adding the given code in the XML file.
Thanks.