我试图创建方法
getProductDetailsHtml
的插件
\Magento\Catalog\Block\Product\ListProduct
但是这个插件无效。
di.xml
<type name="Magento\Catalog\Block\Product\ListProduct">
<plugin name="changes_template_of_product_list" sortOrder="1" disabled="false"
type="StackExchange\Magentodemo\Plugin\Block\ListProductPlugin"/>
</type>
Plugin class
<?php
namespace StackExchange\Magentodemo\Plugin\Block;
class ListProductPlugin
{
public function aroundGetProductDetailsHtml(
\Magento\Catalog\Block\Product\ListProduct $subject,
\Closure $proceed,
\Magento\Catalog\Model\Product $product)
{
$result = $proceed($product);
$result. 'Test';
return $result. 'Test';
}
}
现在,如果我改变了di的Class程.和插件类
Magento\Catalog\Block\Product\AbstractProduct
来自
\Magento\Catalog\Block\Product\ListProduct
那个插件工作得很好。
Now Question:
为什么插件不能用于
ListProduct
在
AbstractProduct
工作的地方
?
如果我们
do di compile
然后会找到
that both class's Interceptor class
有
getProductDetailsHtml()
方法。所以
it means that we can create plugin at getProductDetailsHtml method at both class
Interceptor class of \Magento\Catalog\Block\Product\AbstractProduct
<?php
namespace Magento\Catalog\Block\Product\ListProduct;
/**
* Interceptor class for @see \Magento\Catalog\Block\Product\ListProduct
*/
class Interceptor extends \Magento\Catalog\Block\Product\ListProduct implements \Magento\Framework\Interception\InterceptorInterface
{
/**
* {@inheritdoc}
*/
public function getProductDetailsHtml(\Magento\Catalog\Model\Product $product)
{
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'getProductDetailsHtml');
if (!$pluginInfo) {
return parent::getProductDetailsHtml($product);
} else {
return $this->___callPlugins('getProductDetailsHtml', func_get_args(), $pluginInfo);
}
}
/**
* {@inheritdoc}
*/
public function getImage($product, $imageId, $attributes = array())
{
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'getImage');
if (!$pluginInfo) {
return parent::getImage($product, $imageId, $attributes);
} else {
return $this->___callPlugins('getImage', func_get_args(), $pluginInfo);
}
}
}
Interceptor class of \Magento\Catalog\Block\Product\AbstractProduct
<?php
namespace Magento\Catalog\Block\Product\AbstractProduct;
/**
* Interceptor class for @see \Magento\Catalog\Block\Product\AbstractProduct
*/
class Interceptor extends \Magento\Catalog\Block\Product\AbstractProduct implements \Magento\Framework\Interception\InterceptorInterface
{
/**
* {@inheritdoc}
*/
public function getProductDetailsHtml(\Magento\Catalog\Model\Product $product)
{
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'getProductDetailsHtml');
if (!$pluginInfo) {
return parent::getProductDetailsHtml($product);
} else {
return $this->___callPlugins('getProductDetailsHtml', func_get_args(), $pluginInfo);
}
}
}
但是
really strange that the plugin
没有在
ListProduct
上工作
类.应该在哪里工作。
另外,我正在避免创建
plugin
在
AbstractProduct
因为此类已从版本101.1.0弃用。
So, anybody has idea Why Plugin is not working on ListProduct 's getProductDetailsHtml() ?
- 2019-12-51 #
我现在能想到的唯一原因:班级
\Magento\Catalog\Block\Product\ListProduct
没有使用,但其他一些类继承了AbstractProduct
,也许是通过偏好.你能查一下吗?