Get product collection from a category id

  • I am trying to get a collection of products from a category id. A few things I have tried are in the block :

     $category = Mage::getModel('catalog/category')->load(123)


        $category = Mage::getModel('catalog/category')->load(123);
        $products = $category->getProductCollection()->addCategoryFilter($category)
                             ->addAttributeToFilter('type_id', 'simple')

    also tried just doing it from the phtml

    $oCatId = Mage::getModel('catalog/category')->load(769); 

    None of this works but I am not seeing any errors either. I saw another post that seems like the same question : Magento - Get Products from Specific Category but that method did not work for me either. Thanks for any help!

  • Try this:

    $products = Mage::getModel('catalog/category')->load($category_id)
     ->addAttributeToSelect('*') // add all attributes - optional
     ->addAttributeToFilter('status', 1) // enabled
     ->addAttributeToFilter('visibility', 4) //visibility in catalog,search
     ->setOrder('price', 'ASC'); //sets the order by price


    This should work because it adds the category filter for you by virtue of already having the category loaded:


    public function getProductCollection()
        $collection = Mage::getResourceModel('catalog/product_collection')
        return $collection;

    Wait, it still doesn't work!

    Ok, so you may have larger issues, presumably something overwriting getProductCollection. So let's try circumventing that convenience method:

    $category = Mage::getModel('catalog/category')->load($category_id);
    $products = Mage::getResourceModel('catalog/product_collection')

    Thanks for your help! I feel closer but still floundering. Using your last method, I added `foreach ($products as $product) { echo $product->getId(); }` and I do get a series of ids. However when I try `echo $product->getName();` nothing. I tried adding `->addAttributeToSelect('*')` also but nope. Thanks for any more help.

    @philwinkle Is there a way to get all products for a category? I'm having trouble getting ones that are disabled. I'm using `Mage::getResourceModel('catalog/product_collection')->setStoreId(Mage::app()->getStore()->getId())->addCategoryFilter($category)->addAttributeToSelect('sku');`

  • How to load a product collection with all data that you usually need for product lists in the frontend:

    $_categoryId = 123;
    $category = Mage::getModel('catalog/category')->load($_categoryId );
    $productCollection = $category->getProductCollection();

    This prepares the product collection to load the necessary data to display prices, the product link and any attributes configured as "used in product listing", but not more.

    How can I limit the results? `->limit(5)` didn't work.


    @fschmengler you miss `;` after `$category = Mage::getModel('catalog/category')->load(123)` code

  • This below code will give you product collection from category id 10.

    $categoryId = 10;    
    $products = Mage::getSingleton('catalog/category')->load($categoryId)

    why to use singleton?

  • Code to get product collection from particular category id:

    $productCollection = Mage::getResourceModel('catalog/product_collection')

    perfect!!! +1 :)

    where $category=$categoryId?

  • Below piece of code is much simpler and efficient than loading product collection and filtering using it's attributes,

    $categoryId = 32; // Replace with your category
    $category = Mage::getModel('catalog/category')
    Mage::register('current_category', $category);
    $products = Mage::getSingleton('catalog/layer')->getProductCollection();
    echo $products->getSize();
  • In Magento 2 try this forget category collection using category ID

      $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
      $subCategory =  $objectManager->create('Magento\Catalog\Model\Category')-> 
      foreach($subCategory as $subcat)
  • <?php
    $categoryid = 123; // Category Id
    $category = new Mage_Catalog_Model_Category();
    $collection = $category->getProductCollection();
    foreach ($_productCollection as $_product) { ?>
        <div class="pr_section">
            <div class="pr_desc">
                <a href="<?php echo $_product->getProductUrl(); ?>"><?php echo $_product->getName(); ?></a>
                <?php echo $this->getReviewsSummaryHtml($_product, false, true); // Reviews ?>
                    <?php echo $this->getPriceHtml($_product, true) ?>
                    <?php echo $_product->getShortDescription();?>
                    <?php if(!$_product->canConfigure() && $_product->isSaleable()): ?>
                        <p><button type="button" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Add to Cart')) ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button></p>
                    <?php elseif($_product->getStockItem() && $_product->getStockItem()->getIsInStock()): ?>
                        <p><a title="<?php echo $this->__('View Details') ?>" class="button btn-cart" href="<?php echo $_product->getProductUrl() ?>"><?php echo $this->__('View Details') ?></a></p>
                    <?php else: ?>
                        <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                    <?php endif; ?>
            <div class="pr_img">
                    <a href="<?php echo $_product->getProductUrl() ?>"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(200); ?>" width="200" height="200" alt="" /></a> 
    <?php } ?>

    Get product collection with detail. Hope it will be helpful.

    I think necroposting wasn't necessary as there are enough answers to this question. Furthermore, your use of `new Mage_Catalog_Model_Category()` is questionnable.

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM