We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

    To customize the layered navigation in Magento 2, sometimes, we need to override the core class file. Generally, we can use preference to override models, but it’s not possible every time.

    So on that case, we need to use a different approach and have to use virtualType to achieve that.

    Please find below the steps to override models using virtualType.

    1. Create an di.xml in : /app/code/Magemonkeys/LayerModel/etc

    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <virtualType name="categoryFilterList" type="MagemonkeysLayerModelModelLayerFilterList">
          <arguments>
             <argument name="filterableAttributes" xsi:type="object">MagentoCatalogModelLayerCategoryFilterableAttributeList</argument>
          </arguments>
        </virtualType> 
    </config>

    2. Create an FilterList.php in : /app/code/Magemonkeys/LayerModel/Model/Layer

    <?php
     namespace MagemonkeysLayerModelModelLayer;
     class FilterList extends MagentoCatalogModelLayerFilterList
     {
         public function getFilters(MagentoCatalogModelLayer $layer)
         { 
           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;
       }
     }
    ?>
    field_5bfb909c5ccae

      Get a Free Quote