【问题标题】:How to override Magento\Catalog\Model\Layer\FilterList in magento 2.2.3如何在 magento 2.2.3 中覆盖 Magento\Catalog\Model\Layer\FilterList
【发布时间】:2018-10-04 21:31:34
【问题描述】:

使用插件覆盖 Magento 2.2.3 中的 Magento\Catalog\Model\Layer\FilterList。这个错误来了

PHP 消息:PHP 致命错误:未捕获的 TypeError:参数 2 通过 ####\Plugin\Model\Layer\FilterList::aroundGetFilters() 必须 实现接口 Magento\Catalog\Model\Layer\FilterableAttributeListInterface,实例 给定的闭包,被调用

/magento/framework/Interception/Interceptor.php 在第 135 行并在 ####/Plugin/Model/Layer/FilterList.php:70 中定义

首选项不适用于此文件。

【问题讨论】:

  • 你的问题太宽泛了。请在此处查看如何提出好的问题:stackoverflow.com/help/how-to-ask
  • 听起来你创建了一个闭包而不是一个实现相关接口的类......但是没有看到任何代码,很难提供任何建设性的建议。
  • 分享你尝试过的代码。
  • 我解决了这个问题。
  • 我在使用插件时遇到同样的问题,@NandhiniNagaraj 如何解决?

标签: php magento filter magento2 fatal-error


【解决方案1】:

使用 virtual_type

一次验证di.xml参数传递的核心文件

..../modulename/extensionmodule/etc/frontend/di.xml

 <virtualType name="categoryFilterList" type="ModuleName\ExtensionModule\Model\Layer\FilterList">
    <arguments>
    Your Passing Arguments(same as existing core file - di.xml(core module) passing arguments ).

    </arguments>
</virtualType>

..../modulename/extensionmodule/Model\Layer

<?php

    namespace ModuleName\ExtensionModule\Model\Layer;
    
    /**
     * Override FilterList Class
     */
    class FilterList extends \Magento\Catalog\Model\Layer\FilterList
    {
        ......Your Code Here......
    }
    ?>

【讨论】:

    【解决方案2】:

    这里是覆盖 Magento\Catalog\Model\Layer\FilterList 这个类的完整代码

    我可以使用 virtualType 方法替代 preference

    registration.php

    <?php
    \Magento\Framework\Component\ComponentRegistrar::register(
        \Magento\Framework\Component\ComponentRegistrar::MODULE,
        'Pradip_LayerModel',
        __DIR__
    );
    

    etc/module.xml

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
        <module name="Pradip_LayerModel" setup_version="0.1.0"/>        
    </config>
    

    etc/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">
    
        <virtualType name="categoryFilterList" type="Pradip\LayerModel\Model\Layer\FilterList">
            <arguments>
                <argument name="filterableAttributes" xsi:type="object">Magento\Catalog\Model\Layer\Category\FilterableAttributeList</argument>
            </arguments>
        </virtualType>    
    </config>
    

    模型/层/FilterList.php

    <?php
    
        namespace Pradip\LayerModel\Model\Layer;
    
        /**
         * Override FilterList Class
         */
        class FilterList extends \Magento\Catalog\Model\Layer\FilterList
        {
            public function getFilters(\Magento\Catalog\Model\Layer $layer)
            {
                //echo "you can write code changes here"; exit;
                if (!count($this->filters)) {
                    $this->filters = [
                        $this->objectManager->create($this->filterTypes[self::CATEGORY_FILTER], ['layer' => $layer]),
                    ];
                    foreach ($this->filterableAttributes->getList() as $attribute) {
                        $this->filters[] = $this->createAttributeFilter($attribute, $layer);
                    }
                }
                return $this->filters;
            }
        }
        ?>
    

    【讨论】:

      【解决方案3】:

      您不能通过插件覆盖该类。如果您想覆盖该类,那么只需在您的 di.xml 中使用以下代码

      <preference for="Magento\Catalog\Model\Layer\FilterList" type="NAMESPACE\YOUR_MODULE\Model\Layer\FilterList" />
      

      如果这不起作用,则转到父类 (Magento\Catalog\Model\Layer\FilterList) 并检查您从模块的 FilterList.php 的 __contruct 函数发送的参数的数量。将相同数量的参数以相同的顺序传递给您的父类。它会起作用的。

      【讨论】:

        猜你喜欢
        • 2013-08-01
        • 2023-03-29
        • 1970-01-01
        • 1970-01-01
        • 2018-06-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-16
        相关资源
        最近更新 更多