18

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)
        ->getProductCollection();

and

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

also tried just doing it from the phtml

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

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!

32

Try this:

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

Source: http://overlycaffeinated.com/blog/2011/02/get-all-sale-products-from-a-category-in-magento/

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

Mage_Catalog_Model_Category

public function getProductCollection()
{
    $collection = Mage::getResourceModel('catalog/product_collection')
        ->setStoreId($this->getStoreId())
        ->addCategoryFilter($this);
    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')
        ->setStoreId(Mage::app()->getStore()->getId())
        ->addCategoryFilter($category);
  • 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. – Zac Jul 17 '13 at 21:27
  • @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'); – Nick Rolando Jul 16 '14 at 22:30
9

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();
$productCollection
        ->addStoreFilter()
        ->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds())
        ->addMinimalPrice()
        ->addFinalPrice()
        ->addTaxPercents()
        ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
        ->addUrlRewrite();

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. – Pedram Behroozi Oct 20 '15 at 13:47
  • ->setPageSize(5) – Fabian Schmengler Oct 20 '15 at 13:48
  • @fschmengler you miss ; after $category = Mage::getModel('catalog/category')->load(123) code – Murtuza Zabuawala Dec 7 '16 at 6:52
5

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

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

Code to get product collection from particular category id:

$productCollection = Mage::getResourceModel('catalog/product_collection')
                       ->addCategoryFilter($category);
  • perfect!!! +1 :) – SagarPPanchal Aug 30 '17 at 7:23
  • where $category=$categoryId? – lorakeen Dec 20 '18 at 8:19
2

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')
                 ->setStoreId(Mage::app()->getStore()->getId())
                 ->load($categoryId);

Mage::register('current_category', $category);

$products = Mage::getSingleton('catalog/layer')->getProductCollection();

echo $products->getSize();
0

In Magento 2 try this forget category collection using category ID

  $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  $subCategory =  $objectManager->create('Magento\Catalog\Model\Category')-> 
         load('categorey_id');
  foreach($subCategory as $subcat)
   { 
        print_r($subcat->getData();
  }
-2
<?php

$categoryid = 123; // Category Id

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

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>
        <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> 
        </div>
<?php } ?>

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

  • 1
    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. – Julien Lachal Dec 7 '16 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.