0

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="Magento\Catalog\Model\ResourceModel\Attribute">
    <plugin name="do_stuff_after_attribute_save" type="Vendor\Module\Plugin\Model\ResourceModel\Attribute\Save" />
</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?

1
  • How can you create the plugin of save method when there is no save method in vendor/magento/module-catalog/Model/ResourceModel/Attribute.php file? Jun 20 '18 at 13:12
0

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 Magento\Catalog\Controller\Adminhtml\Product\Attribute\Save 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="Magento\Catalog\Controller\Adminhtml\Product\Attribute\Save">
        <plugin disabled="false" name="Milandev_Admin_Product_Attribute_Save" sortOrder="10" type="Milandev\Testplugin\Plugin\Catalog\Controller\Adminhtml\Product\Attribute\Save"/>
    </type>
</config>

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

<?php
namespace Milandev\Testplugin\Plugin\Catalog\Controller\Adminhtml\Product\Attribute;

class Save
{

    public function afterExecute(
        \Magento\Catalog\Controller\Adminhtml\Product\Attribute\Save $subject,
        $result
    ) {
        die('hello world!');
        //Your plugin code
    }
}

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.