5

I have customized(override) product list collection getProductCollection and filtered collection as required.

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;
        }

        /* custom filter conditions */
        $productUniqueIds = array(10,11,12,13);
        $collection->addAttributeToFilter('entity_id',array('in' => $productUniqueIds));
        /* custom filter conditions end here */

        return $collection;
    }

Based on above products conditions, My result for listing page will be fine but layered navigation result is not proper, displays wrong filter attributes.

I am facing an issue in the layered navigation product count after applying filters on product collection. All counts are wrong. What I guess is I need to apply custom filters on layered navigation's collection as well, but I am not able to find it so far.

I have debugged and reached till _createItem function in vendor/magento/module-catalog/Model/Layer/Filter/AbstractFilter.php file. Product counts are coming from there, but I couldn't find from where exactly product collection gets returned.

Please share your ideas if you have. Any help would be appreciated. Thanks

1

This is a Magento Bug, You can see here : https://github.com/magento/magento2/issues/7730

You can take a pull changes from here : https://github.com/magento/magento2/pull/10246/files

Apply changes and check your count issue, Hope it might be useful for you.

0

Please have a look on classs

Magento\CatalogSearch\Model\Layer\Search\Plugin\CollectionFilter

On this plugin class at metod afterFilter, Magento add query search by below code

$collection->addSearchFilter($query->getQueryText());

This comment this code and see what happen.

1
  • Hello @AmitBera, It looks like collection doesn't filter from this function cause for debug I just added exit(); on top of function but page still works and nothing changed. Jun 18 '18 at 7:28
0

Please try to override getProductCollection() of this class

/var/www/html/m222/vendor/magento/module-catalog/Model/Layer.php

Set you custom collection here and then check.

4
  • We have already overide getProductCollection() function Jun 18 '18 at 11:12
  • @Rakesh Jesadiya You have to override collection of product. Need to try collection of Layer. Jun 18 '18 at 11:13
  • @DharmendraJadav I have overrides Layer.php's getProductCollection but the layernavigation shows wrong data for filter options and wrong toolbar's count I have M2.4.1 and elastic search. Any hint or solution? I even haven't found any blog on it. All links that I have found is not working with M 2.4 version
    – Yogita
    Jun 11 at 4:26
  • @RakeshJesadiya I have overrides Layer.php's getProductCollection but the layernavigation shows wrong data for filter options and wrong toolbar's count I have M2.4.1 and elastic search. Any hint or solution? I even haven't found any blog on it. All links that I have found is not working with M 2.4 version. I checked your link rakeshjesadiya.com/… but it is not working for M 2.4 as some files are not present in M2.4
    – Yogita
    Jun 11 at 4:26

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.