1

I am trying to remove configurable products from product collection, as a result products are being filtered correctly but pagination is not working as expected and showing wrong count.

For example there are 30 products to be listed out of them there are 20 simple and 10 configurable. After making this change I am able to see only simple products on the listing page but sill count shows as 30 which should be 20.

Below is the change I have made in Magento\Catalog\Model\Layer using preference and plugin both but pagination is not working as expected. Below is my logic in the class mentioned:

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

        $collection->addAttributeToFilter('type_id', ['neq'=>"configurable"]);
        return $collection;
    }

I am using version 2.4.1-p1. Please let me know how I can manage this.

2
  • Can you please update question with what issue exactly you are facing with pagination. – Rahul Barot Mar 26 at 10:41
  • @RahulBarot I have updated the question – DEEP JOSHI Mar 30 at 4:35
-1

I suggest you change the visibility of the products to either Search if you still want them to be shown in the search results page OR check this thread "Not Visible Individually" products disaply on Frontend in Magento2.4 where I detailed some workarounds to exclude products from listing, but still have them available at direct access by its URL.

2
  • it can be done via configuration, but I need to hide it on listing page programatically where I am facing issues with pagination count. – DEEP JOSHI Mar 26 at 10:25
  • you will have to intervene in the place where the query is performed before it reaches Elasticsearch. any of the navigation or search features go through ES, the item IDs are returned from ES and the count you see and the LN options are related to that. – Diana Botean Mar 26 at 11:50
-1

You can add product collection filters in below path vendor/magento/module-catalog/view/frontend/templates/product/list.phtml

<?php  if($_product->getTypeId() != "configurable"): ?>

add this filter in your code so the configurable product will not appear on the list page.

1
  • Yes, but pagination count will not work, how to handle that? – DEEP JOSHI Mar 30 at 7:43

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.