I've successfully override productList block to show my custom filtered product collection like this:


<preference for="\Magento\Catalog\Block\Product\ListProduct" type="\Vendor\Module\Block\Product\ListProduct"/>


protected function _getProductCollection()
    $productCollection = parent::_getProductCollection(); 
        $customInput = $_GET['custom_input'];
        $productCollection = $this->myModuleHelper->filterProduct(customInput, productCollection);
    $this->_productCollection = $productCollection;
    return $proCollection;

but the layered navigation options not showing the right option and product count like the image below:

example failed result

1 Answer 1


For making the product collection compatible with the layered nevigation you can follow below code -

protected function _getProductCollection() 
if ($this->_productCollection === null) 
$layer = $this->getLayer(); 
if ($this->getShowRootCategory()) 

        if ($this->_coreRegistry->registry('product')) {
          $categories = $this->_coreRegistry->registry('product')
                    ->getCategoryCollection()->setPage(1, 1)
          if ($categories->count()) {
        $origCategory = null;
        if ($this->getCategoryId()) {
          try {
              $category = $this->categoryRepository->get($this->getCategoryId());
          } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
              $category = null;
          if ($category) {
              $origCategory = $layer->getCurrentCategory();

        $productcollection = $layer->getProductCollection();
        /* you may apply the your custom filter on this product collection */

        $this->_productCollection = $productcollection;

        if ($origCategory) {
    return $this->_productCollection;

In your code neither you are getting the product collection from layer and nor assigning the same to category. For reference you may follow the core block - Magento\Catalog\Block\Product\ListProduct

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.