6

Given a plugin defined in di.xml such as the following:

<type name="\Magento\Catalog\Model\Product">
    <plugin name="se_catalog_product_plugin_ample"
            type="Example\Module\Model\Plugin\Catalog\Product"
            sortOrder="10"
            disabled="false" />
</type>

How can I disable this defined plugin programatically (so not via di.xml but in code)? So when Magento comes round to processing the se_catalog_product_plugin_ample plugin, it is disabled, as if the node above had disabled="true".

I need to be able to alter this plugin programatically due to the requirement of reacting to store config differing in seperate store views in use in the environment. This config can't be reacted upon in di.xml (as far as I know).

Not sure if this is even possible. I know it would be possible to add logic inside the plugin to just $proceed() based on whatever condition needed, but this is one module wanting to disable another that I don't want to have tightly coupled. i.e. the module Example_Foo wants to disable a plugin in Example_Module.

12

In your di.xml you can do the following:

<type name="\Magento\Catalog\Model\Product">
    <plugin name="se_catalog_product_plugin_ample" disabled="true" />
</type>

Magento does it out of the box in Magento/Backend/etc/adminhtml/di.xml with the following :

<type name="Magento\Framework\App\Action\Action">
    <plugin name="storeCheck" disabled="true" />
</type>

Side note: you may have to make your module depends on the Example\Module ;)

  • Thanks for your answer Raphael, appeciate it. I need to be able to do this programatically though, rather than altering the disabled attribute in the di.xml of the other module from the disabler module. I'm going to need to disable the plugin based on system config differing per store. Your answer is actually what I'm doing right now in the wild and does work, but it's a stop gap as in the future I'll need the programatic approach. – Josh Davenport Nov 17 '16 at 17:25
  • Useful tip: you can check all possible plugin XML attributes in XSD schema: github.com/magento/magento2/blob/… – Roman Snitko May 8 at 10:40
0

Not sure if this can be a good idea but you can write an around plugin to that plugin and stop the call. Do at your own risk. Magento recommends avoid around plugins. But seems this can be a perfect purpose.

<type name="\Magento\Catalog\Model\Product">
    <plugin name="se_catalog_product_plugin_ample"
            type="Example\Module\Model\Plugin\Catalog\Product"
            sortOrder="10"
            disabled="false" />
</type>

So lets suppose, In Example\Module\Model\Plugin\Catalog\Product this file there is a function define which is like beforeDelete(its an actual function in product model which cleans cache..just take it as demo). Now write a new plugin aroundBeforeDelete.

public function __construct(
    \Magento\Framework\App\Request\Http $request
    ) {
        $this->request = $request;
    }

    public function aroundBeforeDelete(
        \Magento\Catalog\Model\Product $subject,
        \Closure $proceed,
    ) {

        $this->request->getControllerName();
        // set your condition here or whatever your code
        if($this->request->getControllerName() != 'whatever'){
            $result = $proceed();
            return $result;
        }
    }
  • The original method (in this example \Magento\Catalog\Model\Product::delete) would never execute so you can't use this to skip just one plugin - only all plugins and the original functionality – Josh Davenport Sep 11 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.