How To

How to Remove Block from Any Page using Observer in Magento 2

Hello Magento Friends,

In today’s blog, I am going to show How to Remove a Block from Any Page using Observer in Magento 2.

Blocks are used to display pieces of content placed anywhere on the page. Blocks contain text, image or video. Blocks can be added and removed anytime when required. Usually while adding or removing blocks, you need to modify the layout XML file. But sometimes users need to remove the block without modifying the layout file.

Let’s learn How to Remove Block using Observer in Magento 2.

Steps to Remove Block from Any Page using Observer in Magento 2

Step 1:  We need to create an “events.xml” file inside our extension at the following path

app\code\Vendor\Extension\etc\

Then add the code as follows

<?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_generate_blocks_after">
        <observer name="removeblock" instance="Vendor\Extension\Observer\Removeblock" shared="false" />
    </event>
</config>

Step 2:  After that, we need to create a “Removeblock.php” file inside our extension at the following path.

app\code\Vendor\Extension\Observer\

Now, append the below code

<?php
namespace Vendor\Extension\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class Removeblock implements \Magento\Framework\Event\ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $layout = $observer->getLayout();
        $your_condition = 1; // write your conditional logic here
        if($your_condition == 1)
        {
                // you need to modifed block name as per your requirement.
                $layout->unsetElement('customer-account-navigation-orders-link');
        }
    }
}

Conclusion:

This way you can easily Remove Block from Any Page using Observer in Magento 2. Also checkout, How to Remove Blocks From Layout Magento 2.

If you have any doubt regarding the above-mentioned steps, connect with me through the comment section. Share the article with your Magento colleagues and stay in touch with us

Happy Reading!

Click to rate this post!
[Total: 3 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

6 Innovative Tools Revolutionizing E-Commerce Operations

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

23 hours ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

23 hours ago

Magento 2: How to Add Header and Footer in Checkout

Hello Magento Friends, In today’s blog, we will discuss adding a header and footer to…

2 days ago

Understanding Flexbox Layout in React Native

Hello React Native Friends, Building a visually appealing and responsive mobile app is crucial in…

4 days ago

HYVÄ Themes Releases: 1.3.6 & 1.3.7 – What’s New

We're thrilled to announce the release of Hyvä Themes 1.3.6 and 1.3.7! These latest updates…

4 days ago

How Modern E-Commerce Platforms Leverage Docker & Kubernetes for Scalability

Your e-commerce platform is surging - orders are rolling in, traffic spikes are becoming the…

5 days ago