0

I want to override category product collection on the category product page in front-end. I want to show collection according to attribute set. After customize collection layered navigation product count is not updating.

I am using plugin for modified default category product collection. I am using getProductCollection(). After plugin category product collection filtered by attribute set id but layered navigation is still showing same count. The custom condition is not applied on the layered navigation

Here is my code:

app/code/Sunarc/ProductListpage/etc/frontend/di.xml

<type name="Magento\Catalog\Model\Layer">
    <plugin name="seedolabs-catalog-layer-model" type="Sunarc\ProductListPage\Plugin\Model\Layer"/>
</type> 

app/code/Sunarc/ProductListpage/Plugin/Model/Layer.php

 public function afterGetProductCollection(\Magento\Catalog\Model\Layer $subject, $result){ $result->addAttributeToFilter('attribute_set_id',['eq'=>9]);
return $result;}

kindly help me

Screenshot for issue

Layered navigation issue

8
  • Please use beforeGetProductCollection and add filter over collection May 18 '21 at 7:18
  • I tried but not working can you please send me code so I can check
    – Aish
    May 18 '21 at 11:26
  • you are using elasticsearch ? May 18 '21 at 11:43
  • Yes, I am using elasticsearch 7
    – Aish
    May 18 '21 at 11:45
  • Any suggestion on this issue –
    – Aish
    May 20 '21 at 5:59
0

You have a problem with plugin as @Arun pointed in the comment.

This is core getProductionCollection function:

public function getProductCollection()
    {
        if (isset($this->_productCollections[$this->getCurrentCategory()->getId()])) {
            $collection = $this->_productCollections[$this->getCurrentCategory()->getId()];
        } else {
            $collection = $this->collectionProvider->getCollection($this->getCurrentCategory());
            $this->prepareProductCollection($collection);
            $this->_productCollections[$this->getCurrentCategory()->getId()] = $collection;
        }

        return $collection;
    }

As you can read, the collection is stored inside _productCollections property, so it could be used after creation for other purposes like counting products inside toolbar.

I suggest you to use around plugin on prepareProductCollection method, so you can change your collection there:

<?php

namespace Sunarc\ProductListPage\Plugin\Model;

class Layer
{

    /**
     * @param \Magento\Catalog\Model\Layer $subject
     * @param callable $proceed
     * @param Collection $collection
     */
    public function aroundPrepareProductCollection(\Magento\Catalog\Model\Layer $subject, callable $proceed, $collection)
    {
        $collection->addAttributeToFilter("name", ["like" => "D%"]);
        return $proceed($collection);
    }
}

This plugin will add attribute filter by name starting with letter D to your collection.

5
  • I tried but not working. Product collection has been override but layered navigation product counts still not updating.
    – Aish
    May 18 '21 at 8:38
  • I've tested that on core 2.4.2 and it's working. Do you have any third party modules that changes that behaviour?
    – dudzio
    May 18 '21 at 8:48
  • no I don't have any third party module. Can you please share code segment so I can check
    – Aish
    May 18 '21 at 8:54
  • Above screenshot showing issue . After apply your solution layered navigation issue has been not resolved . We are using elasticsearch 7
    – Aish
    May 18 '21 at 11:20
  • Any suggestion on this issue
    – Aish
    May 20 '21 at 5:59
0

Your code not work might because the product collection loaded somewhere, during Toolbar repair collection, you should create a custom filter append to the Layered Navigation.

You might check the Layer resolver:

  • vendor/magento/module-catalog/Model/Layer.php:147
  • $this->prepareProductCollection($collection);

UPDATE:

  • Based on your detail, you are using elasticsearch, thus, you have to customize the request from Magento post to the ElasticSearch and filter it from ElasticSearch instead of filter the product collection from Magento.
5
  • Product is showing according to the filter but layered navigation is not working according to custom filter after add filter in to the prepareProductCollection function
    – Aish
    May 18 '21 at 11:25
  • Any suggestion on this issue –
    – Aish
    May 20 '21 at 5:59
  • Updated my answer. Jun 23 '21 at 9:55
  • Have you got any answer?
    – ZealousWeb
    Oct 5 '21 at 14:56
  • I am facing same issue in layered navigation
    – ZealousWeb
    Oct 5 '21 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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