skip to Main Content

Override the MagentoCatalogModelLayerFilterList in Magento 2.2.3 using plugin. This error came

PHP message: PHP Fatal error: Uncaught TypeError: Argument 2 passed
to ####PluginModelLayerFilterList::aroundGetFilters() must
implement interface
MagentoCatalogModelLayerFilterableAttributeListInterface, instance
of Closure given, called in

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

preference is not working on this file.

3

Answers


  1. Chosen as BEST ANSWER

    Using virtual_type

    Once Verify the core file of di.xml arguments passing

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

     <virtualType name="categoryFilterList" type="ModuleNameExtensionModuleModelLayerFilterList">
        <arguments>
        Your Passing Arguments(same as existing core file - di.xml(core module) passing arguments ).
    
        </arguments>
    </virtualType>
    

    ..../modulename/extensionmodule/ModelLayer

    <?php
    
        namespace ModuleNameExtensionModuleModelLayer;
        
        /**
         * Override FilterList Class
         */
        class FilterList extends MagentoCatalogModelLayerFilterList
        {
            ......Your Code Here......
        }
        ?>
    

  2. You cannot override the class by plugin. If you want to override the class then just use the following code in your di.xml

    <preference for="MagentoCatalogModelLayerFilterList" type="NAMESPACEYOUR_MODULEModelLayerFilterList" />
    

    If this not works then goto the parent class (MagentoCatalogModelLayerFilterList) and check the no of arugments you are sending from __contruct function of your module’s FilterList.php. Pass same number of arguments with same order to your parent class. It will work.

    Login or Signup to reply.
  3. Here is full code to Override MagentoCatalogModelLayerFilterList this class

    I can use virtualType method for override instead of preference

    registration.php

    <?php
    MagentoFrameworkComponentComponentRegistrar::register(
        MagentoFrameworkComponentComponentRegistrar::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="PradipLayerModelModelLayerFilterList">
            <arguments>
                <argument name="filterableAttributes" xsi:type="object">MagentoCatalogModelLayerCategoryFilterableAttributeList</argument>
            </arguments>
        </virtualType>    
    </config>
    

    Model/Layer/FilterList.php

    <?php
    
        namespace PradipLayerModelModelLayer;
    
        /**
         * Override FilterList Class
         */
        class FilterList extends MagentoCatalogModelLayerFilterList
        {
            public function getFilters(MagentoCatalogModelLayer $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;
            }
        }
        ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search