Fresh Disable a plugin in Magento

Video Lesson: Disable a plugin in Magento

Properly disable a plugin in Magento using dependency injection XML configuration.

Published 2 days ago

Share with colleagues:

Lesson Content

Sometimes you need to turn off a plugin that's causing problems or conflicts with your custom code. Magento makes this straightforward by using the same XML configuration format that is used to create plugins.

Let's say we have an existing plugin that we want to disable. Rather than deleting the code within a third-party module, which would make changes hard to track and module upgrades very difficult, we can simply disable it through configuration.

First, we need to identify the plugin we want to disable. You can find this in the module's di.xml file. For example, here's a plugin from the UPS module which modifies the title if the carrier is UPS:

/**
 * Modify title only when UPS is used as carrier
 *
 * @param Subject $subject
 * @param \Magento\Framework\Phrase|string $result
 * @param Status $trackingStatus
 * @return \Magento\Framework\Phrase|string
 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
 */
public function afterGetTitle(Subject $subject, $result, Status $trackingStatus)
{
    if ($trackingStatus->getCarrier() === Carrier::CODE) {
        $result = __('Status Updated On:');
    }
    return $result;
}

Perhaps we just want the default title displayed, even for UPS shipments. We can easily disable this plugin in our own custom module’s di.xml file.

But first, we must find the original class and plugin name in the related di.xml file of the original module.

<!-- ... -->
<type name="Magento\Shipping\Block\DataProviders\Tracking\DeliveryDateTitle">
    <plugin name="ups_update_delivery_date_title" type="Magento\Ups\Plugin\Block\DataProviders\Tracking\ChangeTitle"/>
</type>
<!-- ... -->
  • Class: Magento\Shipping\Block\DataProviders\Tracking\DeliveryDateTitle

  • Plugin Name: ups_update_delivery_date_title

Now, we can reference that class and plugin name in our di.xml file, and add a disabled="true" to the plugin node:

<?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\Shipping\Block\DataProviders\Tracking\DeliveryDateTitle">
        <plugin name="ups_update_delivery_date_title" disabled="true"/>
    </type>
</config>

The key part is the disabled="true" attribute. We reference the same plugin name but don't need to specify the plugin class - just set it as disabled. When these XML files are processed, they are all merged together, and this cascading effect makes the plugin node disabled.

Pro tip: Since these nodes are all merged together, you don't need to specify the type attribute or any other attributes when disabling a plugin. Magento only needs to know the plugin name to disable it.

Since there are changes to XML files, we can flush the cache for our changes to take effect:

bin/magento cache:flush

This approach is cleaner than modifying core code and makes your customizations more maintainable during Magento upgrades.

If you're dealing with execution conflicts, you may want to also consider adjusting the plugin sort order rather than disabling the plugin entirely.