2

I want to show Title of category page based current page and total page. for example: Category Electronics has total 20 page and i am on 2nd page. I want to show 2 out of 20 pages.

I have tried below code.

 $current_page =  $this->request->getParam("p");

 $objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
            $categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory');

            $categoryId = $this->request->getParam("id"); // YOUR CATEGORY ID
            $category = $categoryFactory->create()->load($categoryId);


            $productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
            $collection = $productCollectionFactory->create();

            $collection->addAttributeToSelect('*');
            $collection->addCategoryFilter($category);
            $collection->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
            $collection->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
            $collection->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);


echo $current_page." of "ceil(count($collection)/48);

I have limiter value 48. But i am getting different count of product than actual showing in category page.

1
  • Try to load the collection: $collection->load(); or $collection->count(). Oct 22, 2017 at 10:16

1 Answer 1

4

You need to set page limit and page number in collection. Try following way:


$collection->setCurPage($current_page)->setPageSize(9);
echo $current_page." of ".$collection->getLastPageNumber();

Following line return last page number. For example: you have 10 page then it return 10.

$collection->getLastPageNumber()
2
  • @shoelrana I can use this thing but collection count is not correct I can see total product showing is 65 and getting collection of 190 products.
    – Kul
    Oct 23, 2017 at 6:35
  • Did not work for me getLastPageNumber() it always shows display 1 even thought I have like 10 pages each page has 12 product. Jan 28, 2021 at 13:35

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.