skip to Main Content

I have following action:

http://localhost/admin/catalog/product_attribute/edit/attribute_id/135/key/…/

I would like to do some extra things with attribute after saving.
I have created and registered custom plugin in Vendor/Module/Plugin/Model/ResourceModel/Attribute/Save.php with following content:

class Save
{
    /**
     * @var Config
     */
     protected $config;


    /**
     * @param Config $config
     */
    public function __construct(Config $config, TypeListInterface $typeList)
    {
        $this->config = $config;
    }

    /**
     *
     * @param Attribute $subject
     * @param Attribute $result
     * @return Attribute $result
     *
     */
    public function afterSave(Attribute $subject, Attribute $result)
    {
        # Do something
    }
}

I have also added following entry to di.xml:

<type name="MagentoCatalogModelResourceModelAttribute">
    <plugin name="do_stuff_after_attribute_save" type="VendorModulePluginModelResourceModelAttributeSave" />
</type>

But the plugin seems not to work. Even if I die('somenthing'); or try to log to file, the code is not executed after saving the attribute.

Maybe I am trying to overwrite wrong method?

3

Answers


  1. You could follow the points below:

    1. You should use di.xml in the adminhtml folder as it is a backend issue.
    2. You should override the execute method of this MagentoCatalogControllerAdminhtmlProductAttributeSave controller class.

    File: app/code/Milandev/Testplugin/etc/adminhtml/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="MagentoCatalogControllerAdminhtmlProductAttributeSave">
            <plugin disabled="false" name="Milandev_Admin_Product_Attribute_Save" sortOrder="10" type="MilandevTestpluginPluginCatalogControllerAdminhtmlProductAttributeSave"/>
        </type>
    </config>
    

    File: app/code/Milandev/Testplugin/Plugin/Catalog/Controller/Adminhtml/Product/Attribute/Save.php

    <?php
    namespace MilandevTestpluginPluginCatalogControllerAdminhtmlProductAttribute;
    
    class Save
    {
    
        public function afterExecute(
            MagentoCatalogControllerAdminhtmlProductAttributeSave $subject,
            $result
        ) {
            die('hello world!');
            //Your plugin code
        }
    }
    
    Login or Signup to reply.
  2. I ran into the same issue some time ago. Turned out that there was another plugin installed which tried to handle the exact same class and property. After adding the sortOrder attribute on both di.xml files, with of course a different value for both, everything worked fine.

    Login or Signup to reply.
  3. Try to make sure that your plugin is applied and all other plugins return expected values.

    1. Go to generated/code/Magento/Catalog/Model/ResourceModel/Eav/Attribute/Interceptor.php (if it’s already generated). If not, just enable developer mode (it will be generated automatically) or run php bin/magento setup:di:compile for production mode.
    2. Look for afterSave() method and print the list of available plugins. E.g.
        /**
         * {@inheritdoc}
         */
        public function afterSave()
        {
            $pluginInfo = $this->pluginList->getNext($this->subjectType, 'afterSave');
            echo "<pre>";
            print_r($pluginInfo);
            die;
            if (!$pluginInfo) {
                return parent::afterSave();
            } else {
                return $this->___callPlugins('afterSave', func_get_args(), $pluginInfo);
            }
        }
    
    1. Then you should see the list of enabled plugins (in your case it might be save_swatches_option_params ). Just look for matches in the code and make sure all of them return EXPECTED results. By default, "after" plugins should return the same $result as the original method does. Otherwise, the next plugins will not work correctly, like in your case.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search