cancel
Showing results for 
Search instead for 
Did you mean: 

Failed to override getProductCollection() from Magento\Catalog\Model\Layer.php

Failed to override getProductCollection() from Magento\Catalog\Model\Layer.php

I tried to override the Magento\Catalog\Model\Layer.php to rewrite getProductCollection() method but it didn't work for me. I have tried multiple things and end up writing here as I had no clue what went wrong.

Added in 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">

    <preference for="Magento\Catalog\Model\Layer"
            type="Folio3\FilterCatalog\Model\Layer" />

</config>

I have no clue what I am missing here. The class Folio3\FilterCatalog\Model\Layer extends \Magento\Catalog\Model\Layer

Surprisingly, no errors shown by the compiler. Code compilation was successful. Looking for some expert tips here

Version: Magento 2.4.3-p1

4 REPLIES 4

Re: Failed to override getProductCollection() from Magento\Catalog\Model\Layer.php


please move the di.xml file 'etc/frontend/di.xml' to 'etc/di.xml' and check it.

Re: Failed to override getProductCollection() from Magento\Catalog\Model\Layer.php

In your etc/di.xml add:

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:frameworkSmiley SurprisedbjectManager/etc/config.xsd">
    <virtualType name="categoryFilterList" type="Magemonkeys\LayerModel\Model\Layer\FilterList">
      <arguments>
         <argument name="filterableAttributes" xsi:type="object">Magento\Catalog\Model\Layer\Category\FilterableAttributeList</argument>
      </arguments>
    </virtualType>
</config>

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

8
9
10
11
12
13
14
15
16
17
18
<?php
namespace Magemonkeys\LayerModel\Model\Layer;
class FilterList extends \Magento\Catalog\Model\Layer\FilterList
{
     public function getFilters(\Magento\Catalog\Model\Layer $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;
   }
}
?>

 

Re: Failed to override getProductCollection() from Magento\Catalog\Model\Layer.php

Re: Failed to override getProductCollection() from Magento\Catalog\Model\Layer.php

use the following Setups

create di.xml file in Folder NAMESPACE/YOUR_MODULE/etc

<?xml version="1.0"?>
<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" type="NAMESPACE\YOUR_MODULE\Model\Layer" />
</config>

create Layer.php Model file in Folder

 

NAMESPACE/NAMESPACE/Model/Rewrite/Catalog

<?php

namespace NAMESPACE\NAMESPACE\Model\Rewrite\Catalog;

class Layer extends \Magento\Catalog\Model\Layer
{
    public function getProductCollection()
    {
        // Do your stuff here
        return parent::getProductCollection();
    }

}

For more clue check below link:

https://magento-qa.com/create-module-and-override-magento-catalog-model-layer-php