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
You could follow the points below:
di.xml
in theadminhtml
folder as it is a backend issue.execute
method of thisMagentoCatalogControllerAdminhtmlProductAttributeSave
controller class.File:
app/code/Milandev/Testplugin/etc/adminhtml/di.xml
File:
app/code/Milandev/Testplugin/Plugin/Catalog/Controller/Adminhtml/Product/Attribute/Save.php
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.
Try to make sure that your plugin is applied and all other plugins return expected values.
generated/code/Magento/Catalog/Model/ResourceModel/Eav/Attribute/Interceptor.php
(if it’s already generated). If not, just enabledeveloper
mode (it will be generated automatically) or runphp bin/magento setup:di:compile
for production mode.afterSave()
method and print the list of available plugins. E.g.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.