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

时间:2018-04-24 14:53:45

标签: php magento filter magento2 fatal-error

使用插件覆盖Magento 2.2.3中的Magento\Catalog\Model\Layer\FilterList。出现此错误

  

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

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

首选项不适用于此文件。

3 个答案:

答案 0 :(得分: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......
    }
    ?>

答案 1 :(得分:0)

您无法通过插件覆盖该类。如果要覆盖该类,则只需在di.xml中使用以下代码

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

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

答案 2 :(得分:0)

这里是覆盖Magento \ Catalog \ Model \ Layer \ Filter的完整代码列出此类

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