How do display products by category id in template page(.phtml page) in magento?

How do display products by category id in template page(.phtml page) in magento?

October 6, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How do display products by category id in template page(.phtml page) in magento?

Displaying Products by Category ID in Magento

As a new developer exploring the Magento platform, you’ve already taken an important step by creating a function to retrieve all category IDs along with their names using the catalog/category model. This array of categories forms the foundation for displaying products grouped by their respective categories.

Understanding the Current Code Structure

Your current code creates an associative array where the keys are the category IDs and the values are the corresponding category names. For example:

Array(
    [Root Catalog] => 1
    [Default Category] => 2
    [Multivitamins] => 3
    // ... other categories ...
)

Displaying Products by Category

To display products separated by their respective categories, you’ll need to retrieve the product collection for each category and iterate through them. Here’s a step-by-step approach:

  1. Load the category model: Start by loading the desired category using its ID.
$category = Mage::getModel('catalog/category')->load($categoryId);
  1. Retrieve the product collection: Use the loaded category to get all associated products.
$productCollection = $category->getProductCollection();
  1. Iterate through the products: Loop through each product in the collection and display its details as required.
foreach ($productCollection as $product) {
    echo $product->getName() . '<br>';
    // Display other product attributes like price, image, etc.
}

Displaying Products on a Template Page

Now let’s incorporate this logic into your existing allproducts.phtml template file. We’ll add a loop to display all products grouped by their categories.

<div class="category-products">
    <?php $categories = get_categories(); ?>
    <?php foreach ($categories as $categoryId => $categoryName): ?>
        <h2><?php echo $categoryName; ?></h2>
        <?php
            $category = Mage::getModel('catalog/category')->load($categoryId);
            $productCollection = $category->getProductCollection();
        ?>
        <?php foreach ($productCollection as $product): ?>
            <div class="item">
                <div class="product-info">
                    <a href="<?php echo $this->helper('catalog/product')->getProductUrl($product) ?>"><?php echo $product->getName() ?></a>
                    <!-- Add other product details like image, price, etc. -->
                </div>
            </div>
        <?php endforeach; ?>
    <?php endforeach; ?>
</div>

Conclusion

In this post, you’ve learned how to display products grouped by their respective categories using the Magento catalog/category model and the product collection. By combining your existing function with the logic for retrieving product collections, you can now present a clean and organized view of your products on your store’s frontend.

Remember that there are many ways to optimize this process depending on your specific requirements and performance needs. For more advanced customizations and best practices in Magento development, I recommend exploring the official documentation and resources available from Laravel Company.

Don’t hesitate to reach out if you have any further questions or need assistance with other Magento development tasks!

Catalog Themes Pricing Import/Export

Our network