【问题标题】:Overwriting core block using plugin method使用插件方法覆盖核心块
【发布时间】:2019-08-18 20:22:27
【问题描述】:

我创建了一个模块,我在其中覆盖了产品列表小部件的核心功能。我可以使用偏好来实现这一点,但我想了解插件方法.. 下面是我的 frontend/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\CatalogWidget\Block\Product\ProductsList">
        <plugin name="widget_product_listing_add_attribute" type="Test\CatalogWidget\Plugin\Block\Product\ProductsListAddAttributePlugin" sortOrder="1"/>
    </type>
</config>

下面是我的插件块代码。

namespace Test\CatalogWidget\Plugin\Block\Product;

class ProductsListAddAttributePlugin
{
    public function beforeCreateCollection(\Magento\CatalogWidget\Block\Product\ProductsList $subject, \Magento\Catalog\Model\ResourceModel\Product\Collection $result)
    {
        /**
        * @var \Magento\CatalogWidget\Block\Product\ProductsList $subject
        * @var \Magento\Catalog\Model\ResourceModel\Product\Collection $result
        */

        die('ProductsListAddAttributePlugin before....');
        
    }
}

安装模块并运行 di:compile.. 当我重新加载页面时出现以下错误。

致命错误:未捕获的 ArgumentCountError:参数太少 功能 Test\CatalogWidget\Plugin\Block\Product\ProductsListAddAttributePlugin::beforeCreateCollection(), 1通过 C:\xampp\htdocs\projects\hello\vendor\magento\framework\Interception\Interceptor.php 在第 121 行,正好 2 预计在 C:\xampp\htdocs\projects\hello\app\code\Test\CatalogWidget\Plugin\Block\Product\ProductsListAddAttributePlugin.php:6 堆栈跟踪:#0 C:\xampp\htdocs\projects\hello\vendor\magento\framework\Interception\Interceptor.php(121): Test\CatalogWidget\Plugin\Block\Product\ProductsListAddAttributePlugin->beforeCreateCollection(Object(Magento\CatalogWidget\Block\Product\ProductsList\Interceptor))

1 C:\xampp\htdocs\projects\hello\vendor\magento\framework\Interception\Interceptor.php(153):

Magento\CatalogWidget\Block\Product\ProductsList\Interceptor->Magento\Framework\Interception{closure}()

2 C:\xampp\htdocs\projects\hello\generated\code\Magento\CatalogWidget\Block\Product\ProductsList\Interceptor.php(26):

放大 C:\xampp\htdocs\projects\hello\app\code\Test\CatalogWidget\Plugin\Block\Product\ProductsListAddAttributePlugin.php 在第 6 行

【问题讨论】:

标签: php magento magento2


【解决方案1】:

尝试删除生成的代码 我认为您在 Windows 下工作,您可以在提示终端上运行以下命令。 首先确保你处于开发者模式下

php bin/magento deploy:mode:set developer

php rm -rf generated*
php bin/magento cache:flush
php bin/magento cache:clean
php bin/magento setup:upgrade
bin/magento setup:di:compile

【讨论】:

    【解决方案2】:

    您运行setup:di:compile 并清除缓存了吗?更新beforeCreateCollection 方法后?

    看起来您正确地传递了两个参数,但它可能指的是较旧的 DI。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-06
      相关资源
      最近更新 更多