If you own a Magento online store, it is important that you design the store and its navigational features that make it easy for visitors to find the products they want and shop easily.
When you offer a large number of products, it is not possible to display all of them at once. Also, loading all products at once takes time which results in script time-out.
The solution to the problem is load products on scroll or display page wise products collection. Here I’ll give the solution to get page wise products collection in Magento.
Example to Get Page wise Products Collection in Magento:
$p = 0; while (1) { $p++; $products = Mage::getModel('catalog/product') ->getCollection() ->addAttributeToSelect('*') ->setPageSize(200) ->setCurPage($p) ->setOrder('id', 'ASC') ->addAttributeToFilter('status', array('eq' => '1')); foreach ($products as $_product) { echo '<br>'.$_product->getId(); } if ($p >= $products->getLastPageNumber()) { break; } }
When fetching product collections page-wise in Magento, managing the number of items displayed per page is important. You can achieve that using the setPageSize
method in Magento 2 collections.
Hopefully, the above example helps you to get the solution.
Thanks!