6

I am trying to get the collection of visible products on my category page. But as soon as I call getLoadedProductCollection(), my category page shows all products, effectively rendering pagination useless.

My module injects a phtml in the <head>-tag, and it's PHP code is this:

$categoryBlock = $this->getLayout()->getBlock('category.products.list');
$collection = $categoryBlock->getLoadedProductCollection()

Now, if I don't get the loaded products collection, everthing works fine. Magento states 'showing 15 products' and the pagination shows '1-15 of 17'.

However... as soon as my code also wants to have a peek of the product collection by invoking getLoadedProductCollection(), the category page and pagination show all 17 products, even though it's set to a lower value. (It then states 'items 1-17 of 17').

How can I get the same collection as the category page itself? That is: limited by the products that are actually shown?

2
  • Is there any solution for this issue? I am also facing the same issue. Here is my .xml file code. <referenceContainer name="content"> <block class="Magento\Catalog\Block\Category\View" name="category.products" template="Magento_Catalog::category/products.phtml"> <block class="Magento\Catalog\Block\Product\ListProduct" name="lazy" template="Vendor_Module::product/lazylist.phtml"/> </block> </referenceContainer> still, not able to get correct count of records. $_productCollection = $block->getLoadedProductCollection(); echo $_productCollection->count(); Sep 14, 2018 at 7:05
  • I'm Magento2. 4. I have encountered this problem, but I still don't understand how to solve it. Can you explain it specifically? Can you provide a complete example
    – sq y
    Apr 21 at 10:28

2 Answers 2

1

I know I'm 4 years late but recently I stumbled upon the same issue in Magento 1.9. The key to the problem is that we were trying to insert something related to the product collection inside the <head> tag. Apparently the layer must not be fully initialized by then, and so it doesn't know that the page has the size of 15. By calling the collection in the head we must be overwriting the page size. That's why it shows the full collection in the navigation.

As it wasn't critical for me to have the product collection related computations inside the <head>, I moved it to "before_body_end".

Hopefully I can spare somebody some time with my answer ;)

2
  • Had the same issue on M2.4 and with your answer i found the reason. An extension which added an element in the <head> component called the getLoadedProductCollection(). This should be the accepted answer.
    – remklov
    Jul 20, 2021 at 7:08
  • I'm Magento2. 4. I have encountered this problem, but I still don't understand how to solve it. Can you explain it specifically? Can you provide a complete example
    – sq y
    Apr 21 at 10:28
0

This is kind of a hacky solution, but it works:

$categoryBlock = $this->getLayout()->getBlock('category.products.list');
$collection = $categoryBlock->getLoadedProductCollection()

$categoryLayer = $categoryBlock->getLayer();
$rLayer = new \ReflectionClass($categoryLayer);
$rProp = $rLayer->getProperty("_productCollections");
$rProp->setAccessible(1);
$rProp->setValue($categoryLayer, []);

// do whatever you want with $collection from here, ex. call getLoadedProductCollection again

This will reload the collection when you call getLoadedProductCollection for the second time. If the method was already called before this template, then just move your $collection =... line after the comment.

1
  • I'm Magento2. 4. I have encountered this problem, but I still don't understand how to solve it. Can you explain it specifically? Can you provide a complete example
    – sq y
    Apr 21 at 10:28

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.