Magento2插件/拦截器不起作用

时间:2018-06-20 10:46:06

标签: magento magento2

我有以下操作:

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

保存后,我想对属性做一些额外的事情。 我已经在Vendor/Module/Plugin/Model/ResourceModel/Attribute/Save.php中创建并注册了具有以下内容的自定义插件:

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
    }
}

我还向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>

但是该插件似乎无法正常工作。即使我die('somenthing');或尝试登录到文件,保存属性后也不会执行代码。

也许我正在尝试覆盖错误的方法?

3 个答案:

答案 0 :(得分:0)

您可以遵循以下几点:

  1. 您应该在di.xml文件夹中使用adminhtml,因为这是一个后端问题。
  2. 您应该重写此execute控制器类的Magento\Catalog\Controller\Adminhtml\Product\Attribute\Save方法。

文件: 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>

文件: 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
    }
}

答案 1 :(得分:0)

我前段时间遇到了同样的问题。结果发现安装了另一个插件试图处理完全相同的类和属性。在两个 di.xml 文件上添加 sortOrder 属性后,当然两者的值不同,一切正常。

答案 2 :(得分:0)

尽量确保您的插件被应用并且所有其他插件都返回预期值。

  1. 转到 generated/code/Magento/Catalog/Model/ResourceModel/Eav/Attribute/Interceptor.php(如果已生成)。如果没有,只需启用 developer 模式(它会自动生成)或运行 php bin/magento setup:di:compile 以进行生产模式。
  2. 查找 afterSave() 方法并打印可用插件列表。例如
    /**
     * {@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. 然后您应该会看到已启用插件的列表(在您的情况下可能是 save_swatches_option_params )。只需在代码中查找匹配项并确保所有匹配项都返回 EXPECTED 结果。默认情况下,“after”插件应该返回与原始方法相同的 $result。否则,下一个插件将无法正常工作,就像您的情况一样。