Display magento products by category ID

Display magento products by category ID

July 13, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Display magento products by category ID

Displaying Magento Products by Category ID

When working with Magento, you might find yourself needing to display specific products on your cart page or below the total, based on their product IDs. This allows for targeted promotions such as “Frequently Bought Together” or “Customers Also Liked”. Let’s dive into how to achieve this using Magento 2.

Step 1: Understanding the Current Code

You’ve already set up a basic structure that loads products from a specific category and displays their images and names. Here’s a quick breakdown:

<div class="freeProducts voucher code">
    <?php

    $categoryid = 64;

    $category = new Mage_Catalog_Model_Category();
    $category->load($categoryid);
    $collection = $category->getProductCollection();
    $collection->addAttributeToSelect('*');

    foreach ($collection as $_product) {
    ?>

    <a href="<?php echo $_product->getProductUrl() ?>" target="_blank"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(200); ?>" width="200" height="200" alt="" /></a>
    <a href="<?php echo $_product->getProductUrl(); ?>"><?php echo $_product->getName(); ?></a>

    <?php } ?>
</div>

This code loads a category with ID 64, collects all its products, and iterates through them to display their images and names. However, you mentioned the need for “ADD TO CART” buttons and prices.

Step 2: Displaying ADD TO CART Buttons

To add an “Add to Cart” button for each product, we’ll use Magento’s built-in catalog/product_link.phtml template:

<div class="freeProducts voucher code">
    <?php

    $categoryid = 64;

    $category = new Mage_Catalog_Model_Category();
    $category->load($categoryid);
    $collection = $category->getProductCollection();
    $collection->addAttributeToSelect('*');

    foreach ($collection as $_product) {
        // Add this line
        echo $this->getChildHtml('addtobasket');
    ?>

    <a href="<?php echo $_product->getProductUrl() ?>" target="_blank"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(200); ?>" width="200" height="200" alt="" /></a>
    <a href="<?php echo $_product->getProductUrl(); ?>"><?php echo $_product->getName(); ?></a>

    <?php } ?>
</div>

We simply use the getChildHtml('addtobasket') method to include the button. This will automatically handle the “Add to Cart” functionality.

Step 3: Displaying Product Prices

To display prices, we’ll use the built-in catalog/product/price.phtml template:

<div class="freeProducts voucher code">
    <?php

    $categoryid = 64;

    $category = new Mage_Catalog_Model_Category();
    $category->load($categoryid);
    $collection = $category->getProductCollection();
    $collection->addAttributeToSelect('*');

    foreach ($collection as $_product) {
        // Add these lines
        echo $this->getPriceHtml($_product, true);

        // And these two lines for "Add to Cart" button
        echo $this->getChildHtml('addtobasket');
    ?>

    <a href="<?php echo $_product->getProductUrl() ?>" target="_blank"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(200); ?>" width="200" height="200" alt="" /></a>
    <a href="<?php echo $_product->getProductUrl(); ?>"><?php echo $_product->getName(); ?></a>

    <?php } ?>
</div>

The getPriceHtml($_product, true) method displays the product’s price.

Conclusion

Displaying products by category ID in Magento is a straightforward process once you understand how to leverage the built-in templates. This approach keeps your code clean and maintainable while ensuring that all essential functionality is handled correctly.

Remember, Magento 2 offers extensive documentation on its official website (https://laravelcompany.com/magento-2-documentation), which can be an invaluable resource for advanced customizations like this one.

The key takeaways are:

  1. Use Magento’s built-in templates (catalog/product_link.phtml and catalog/product/price.phtml) to add “Add to Cart” buttons and prices.
  2. Ensure your product collection has the necessary attributes selected ($collection->addAttributeToSelect('*');).
  3. Keep your code organized and maintain clean separation of concerns.

With this knowledge, you can now create targeted promotions that enhance the shopping experience on your Magento store.

Magento 2 Checkout Catalog Customers

Our network