1

In my custom module, I'm trying to override Magento\Catalog\Model\Layer\FilterList class (more precisely: I want to override function getFilters).

In the module's etc\di.xml there are:

  1. One plugin statement
  2. One preference statement

The plugin is working perfect, while preference is completely ignored.

    <preference for="Magento\Catalog\Model\Layer\FilterList" type="\vendor\module\Model\Layer\FilterList"/>

<type name="Magento\Catalog\Model\Product">
    <plugin name="logProductName" type="\vendor\module\Plugin\ProductName" />
</type>

I double checked all class references - all's good. Please let me know what I'm missing.

Thank you very much!

7
  • 1
    Why you are overriding this method ? You can use simply plugin feature because this getFilters method is public
    – Keyur Shah
    Commented Nov 10, 2016 at 9:56
  • I know it's public. However, in my case is simplier to use overriding. Any ideas? Commented Nov 10, 2016 at 11:25
  • Just use around plugin and don't call original method($procede() closure) , and do your magic :)
    – Keyur Shah
    Commented Nov 10, 2016 at 11:56
  • Thanks Keyur. Just done it with plugin. I'm not calling the original method. I do my magic ;) - However, I have problem with accessing protected variables. How do I access them? Commented Nov 10, 2016 at 13:51
  • You can define protected variable in your file and Inject \Magento\Catalog\Model\Layer\FilterList into constructor so you can call createAttributeFilter () method
    – Keyur Shah
    Commented Nov 10, 2016 at 13:57

2 Answers 2

4

Here is full code to Override Magento\Catalog\Model\Layer\FilterList this class

I can use virtualType method for override instead of 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;
        }
    }
 ?>
1
  • This is working for me. Commented Oct 25, 2019 at 12:57
0

May be below code is work for you.

 <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
     <preference for="Magento\Catalog\Model\Layer\FilterList" type="Vendor\Module\Model\Layer\FilterList"/>
 </config>

use this instead of

 <preference for="Magento\Catalog\Model\Layer\FilterList" type="\Vendor\Module\Model\Layer\FilterList"/>

and make sure you have to re-compile, or remove var/di, var/generation etc.

and you have to extends this \Magento\Catalog\Model\Layer\FilterList core class in your custom class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.