题
我在自定义模块中创建了一个插件来覆盖默认方法 getName()
在 Magento\Catalog\Model\Product.php
我创建了名为的自定义模块 Firstmodule
里面 app\code\Magento
. 。我正在使用 after
插件的监听器。
文件 app\code\Magento\Firstmodule\etc\frontend\di.xml
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<type name="Magento\Catalog\Model\Product">
<plugin name="Magento_Firstmodule::before" type="Magento\Firstmodule\Model\Basic" sortOrder="1" />
</type>
</config>
和模型 app\code\Magento\Firstmodule\Model\Basic.php
<?php
namespace Magento\Firstmodule\Model;
class Plugin {
public function afterGetName(\Magento\Catalog\Model\Product $subject, $result)
{
return '|' . $result . '|';
}
}
前端产品视图页面仍然没有结果。
谁能告诉我缺少什么。?提前致谢。
解决方案
您已指定 Magento\Firstmodule\Model\Basic 类型,但在 Basic.php 文件中将该类称为“Plugin”。它需要是
class Basic {
}