Advertisement

Facebook

How to Override or Disable Core and Existing Plugin in Magento 2

How to Override or Disable Core and Existing Plugin in Magento 2

In Magento 2, plugins allow you to modify or extend the behavior of existing class methods without altering the core code. If you want to override or disable core plugins (or any existing plugin), there are structured ways to accomplish this, ensuring your implementation is upgrade-safe.

What is a Plugin?

A plugin (also known as an interceptor) is a mechanism in Magento 2 that allows you to intercept method calls for any public method in a class. You can modify the behavior of methods without overriding the class itself. However, there may be scenarios where you need to disable or completely replace an existing plugin (core or third-party).

Disabling a Core or Existing Plugin

To disable a plugin in Magento 2:

  1. Locate the Plugin Configuration
    Check the di.xml file where the plugin is configured. For example, core plugins are typically configured in a module's etc/di.xml file.
    Example:
    <?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\Sales\Model\ResourceModel\Order">
            <plugin name="authorization" type="Magento\Sales\Model\ResourceModel\Order\Plugin\Authorization" />
        </type> 
    </config>
    
  2. Disable the Plugin
    Add a new di.xml file in your custom module and disable the plugin by setting the disabled attribute to true.
    Example di.xml File:
    <?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\Sales\Model\ResourceModel\Order">
            <plugin name="authorization" disabled="true" />
        </type>
    </config>
    
    This will disable the specified plugin (authorization in this case) for the targeted class (Magento\Sales\Model\ResourceModel\Order).

Overriding a Core or Existing Plugin

If you need to override the logic of an existing plugin, follow these steps:

1. Create a Custom Plugin Class

Write a new plugin class that extends the original plugin. For example:

Custom Plugin File: Vendor/Module/Plugin/Order/Authorization.php

<?php

namespace Vendor\Module\Plugin\Order;

class Authorization extends \Magento\Sales\Model\ResourceModel\Order\Plugin\Authorization
{
    /**
     * Override the protected aroundIsAllowed() method.
     *
     * @param \Magento\Sales\Model\Order $order
     * @param \Closure $proceed
     *
     * @return bool
     */
    protected function aroundIsAllowed(\Magento\Sales\Model\Order $order, \Closure $proceed)
    {
        // Custom logic here
        return true; // Example: Allow all orders
    }
}

2. Update di.xml Configuration

In the di.xml file of your custom module, add a new configuration to replace the core plugin with your custom implementation.

Example 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\Sales\Model\ResourceModel\Order">
        <!-- Disable the core plugin -->
        <plugin name="authorization" disabled="true" />

        <!-- Add your custom plugin -->
        <plugin name="custom_authorization" type="Vendor\Module\Plugin\Order\Authorization" sortOrder="1" />
    </type>
</config>

This configuration does the following:

  • Disables the existing plugin (authorization).
  • Registers your custom plugin (custom_authorization) to override the behavior.

Replacing a Plugin Completely

If you don't want to extend the original plugin and instead replace it entirely with your own logic:

1. Disable the core plugin and Register your own implementation with the same name but different logic:

<?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\Sales\Model\ResourceModel\Order">
        <!-- Disable the core plugin -->
        <plugin name="authorization" disabled="true" />

        <!-- Add your custom plugin -->
        <plugin name="authorization" type="Vendor\Module\Plugin\Order\Authorization" sortOrder="1" />
    </type>
</config>

2. Write your custom plugin class:

<?php

namespace Vendor\Module\Plugin\Order;

class Authorization extends \Magento\Sales\Model\ResourceModel\Order\Plugin\Authorization
{
    /**
     * Override the protected aroundIsAllowed() method.
     *
     * @param \Magento\Sales\Model\Order $order
     * @param \Closure $proceed
     *
     * @return bool
     */
    protected function aroundIsAllowed(\Magento\Sales\Model\Order $order, \Closure $proceed)
    {
        // Custom logic here
        return true; // Example: Allow all orders
    }
}

Disabling Third-Party Plugin

If you're working with a third-party plugin:

  1. Identify the Plugin: Look for the plugin definition in the third-party module’s di.xml.
  2. Disable it: Use your custom module's di.xml to disable the plugin using its name.

Points to Note

  1. Sort Order: Plugins have a sortOrder property in di.xml. If multiple plugins are applied to the same method, Magento executes them based on this value. Higher sortOrder values are executed later.
  2. Use Dependency Injection (DI): When writing custom plugins, rely on DI for injecting dependencies instead of directly instantiating classes with ObjectManager.
  3. Test Thoroughly: Ensure that the overridden or replaced logic works as intended and does not break other functionalities.
  4. Avoid Overloading Core Logic: Be mindful of extending or replacing core plugins. Make sure the changes align with Magento's architectural guidelines.

Conclusion

Overriding or disabling core plugins in Magento 2 can be done safely using the di.xml configuration and custom plugin classes. These techniques allow you to implement custom functionality without altering the core codebase. By following the steps outlined above, you can ensure your customizations are maintainable and upgrade-safe.

Let me know if you have any questions or need further assistance! 🚀

Post a Comment

0 Comments

Buy Me A Coffee