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

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

2 Answers 2

1

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
    Commented May 18, 2021 at 11:25
  • Any suggestion on this issue –
    – Aish
    Commented May 20, 2021 at 5:59
  • Updated my answer. Commented Jun 23, 2021 at 9:55
  • Have you got any answer?
    – ZealousWeb
    Commented Oct 5, 2021 at 14:56
  • I am facing same issue in layered navigation
    – ZealousWeb
    Commented Oct 5, 2021 at 14:56
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.

6
  • I tried but not working. Product collection has been override but layered navigation product counts still not updating.
    – Aish
    Commented May 18, 2021 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
    Commented May 18, 2021 at 8:48
  • no I don't have any third party module. Can you please share code segment so I can check
    – Aish
    Commented May 18, 2021 at 8:54
  • Above screenshot showing issue . After apply your solution layered navigation issue has been not resolved . We are using elasticsearch 7
    – Aish
    Commented May 18, 2021 at 11:20
  • Any suggestion on this issue
    – Aish
    Commented May 20, 2021 at 5:59

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.