Magento - Get all products in the current category

Magento - Get all products in the current category

July 12, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - Get all products in the current category

Retrieving All Products in the Current Category in Magento

In Magento, retrieving all products within a specific category across multiple pages can be achieved by using the category collection and pagination. The issue you’re facing is due to the product collection being limited to the page view, which only includes the current page of results.

To get around this limitation, we need to create our own custom product collection that fetches all products in the current category, regardless of the page number. Let me guide you through this process:

Step 1: Create a Magento Block

First, create a new block file under app/code/Local/Company/Module/Block. Name it something like ProductCollection.php. This block will contain the logic to fetch all products in the current category.

class ProductCollection extends Mage_Core_Block_Template
{
    public function getAllCategoryProducts()
    {
        $category = Mage::registry('current_category');
        if ($category instanceof Mage_Catalog_Model_Category) {
            $products = Mage::getModel('catalog/product')->getCollection()
                ->addAttributeToSelect('*')
                ->addCategoryFilter($category)
                ->load();

            return $products;
        }

        return array();
    }
}

In this block, we first retrieve the current category from the registry. Then, we create a new product collection and add all attributes to select. We filter the products by the current category using addCategoryFilter. Finally, we load the collection and return it.

Step 2: Use the Block in Your Template

Now that we have created the block, let’s use it in your template file (e.g., list.phtml).

<?php $products = $this->getChildHtml('product.collection') ?>

In this line of code, we are calling the getChildHtml method to retrieve the output generated by our custom ProductCollection block.

Step 3: Loop Through All Products

Finally, loop through all the products in your template file. You can use the foreach loop as you did before:

<?php foreach ($products as $_product): ?>
    <div class="product-item">
        <!-- Display your product information here -->
    </div>
<?php endforeach; ?>

In this code snippet, we are looping through every product in the $products array returned by our custom block.

Remember to clear all caches and reindex data for changes to take effect. You can do this from the Magento admin panel under System > Cache Management and System > Index Management respectively.

Conclusion

By following these steps, you should now be able to retrieve and display all products within the current category across multiple pages in your Magento store. The key is to create a custom product collection that fetches all products for the specified category instead of relying on the limited page view collection provided by Magento out-of-the-box.

For more detailed guides on Magento development, you can explore our [Magento tutorial series][https://laravelcompany.com/magento-development-tutorials] at LaravelCompany. We provide step-by-step tutorials and best practices to help you master Magento development efficiently.

Happy coding!

Catalog Extensions Themes Errors

Our network