如何在 magento 2.2.3 中覆盖 Magento\Catalog\Model\Layer\FilterList

How to override Magento\Catalog\Model\Layer\FilterList in magento 2.2.3

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

PHP message: PHP Fatal error: Uncaught TypeError: Argument 2 passed to ####\Plugin\Model\Layer\FilterList::aroundGetFilters() must implement interface Magento\Catalog\Model\Layer\FilterableAttributeListInterface, instance of Closure given, called in

/magento/framework/Interception/Interceptor.php on line 135 and defined in ####/Plugin/Model/Layer/FilterList.php:70

偏好不适用于此文件。

您不能通过插件覆盖 class。如果您想覆盖 class 然后只需在 di.xml

中使用以下代码
<preference for="Magento\Catalog\Model\Layer\FilterList" type="NAMESPACE\YOUR_MODULE\Model\Layer\FilterList" />

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

使用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\图层

<?php

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

这是覆盖 Magento\Catalog\Model\Layer\FilterList 这个 class

的完整代码

我可以使用 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>

Model/Layer/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;
        }
    }
    ?>