In Magento 2, observers play a critical role in executing custom code in response to specific events. However, there might be scenarios where you need to remove a particular observer from an event. In this guide, we’ll show you how to achieve this by creating a plugin that modifies the event configuration. Here are the steps:

Method : 1

Step 1: Prepare Your Environment

  1. Make sure you have a working Magento 2 installation.
  2. Create or locate your custom module. If you’re new to module creation, refer to Magento’s official documentation on creating a module.

Step 2: Create the Plugin

  1. In your module’s directory, navigate to etc and create a file named di.xml.
  2. Add the following content to di.xml:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\Event\Config">
        <plugin name="disable_specific_observer" type="[Vendor]\[Module]\Plugin\DisableSpecificObserverPlugin" />
    </type>
</config>

Step 3: Implement the Plugin

  1. In your module’s directory, navigate to Plugin and create a PHP file named DisableSpecificObserverPlugin.php.
  2. Insert the following code into DisableSpecificObserverPlugin.php:
<?php

namespace [Vendor]\[Module]\Plugin;

use Magento\Framework\Event\Config;

class DisableSpecificObserverPlugin
{
    public function afterGetObservers(Config $subject, $observers)
    {
        // Specify the event name and observer name you want to remove
        $eventToRemove = 'checkout_cart_save_after';
        $observerToRemove = 'inventory';

        // Check if the specified event exists in the configuration
        if (isset($observers[$eventToRemove])) {
            // Remove the specified observer from the event configuration
            if (isset($observers[$eventToRemove]['observers'][$observerToRemove])) {
                unset($observers[$eventToRemove]['observers'][$observerToRemove]);
            }
        }

        return $observers;
    }
}

Step 4: Apply Changes

  1. Clear the cache to ensure the changes take effect:
php bin/magento cache:clean

Step 5: Verify the Result

  1. Ensure that the specified event name and observer name match the ones you want to remove.
  2. The observer you specified should be removed from the event configuration. Test your Magento application thoroughly to ensure that the desired observer removal has been achieved.

Method : 2

Step 1: Prepare Your Environment

  1. Make sure you have a working Magento 2 installation.
  2. Create or locate your custom module. If you’re new to module creation, refer to Magento’s official documentation on creating a module.

Step 2: Create the Plugin

  1. In your module’s directory, navigate to etc and create a file named events.xml.
  2. Add the following content to events.xml:
<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_order_item_cancel">
        <observer name="inventory" instance="Magento\CatalogInventory\Observer\CancelOrderItemObserver" disabled="true"/>
    </event>
</config>

In the events.xml file, define your custom observer configuration that disables the default observer for the sales_order_item_cancel event. Set the <disabled> tag to true to disable the default observer.

Thank you so much for taking the time to read my article.

Rate this post