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)
        ->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!

Solutions

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.

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

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

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

Similar questions

Get product Collection with all product with category id
I'm trying to get product collection and join table catalog/category_product. And I have the error. Item (Mage_Catalog_Model_Product) with the same id "338" already exist. What am I doing wrong? Thanks.
How to retrieve the Category name, Category URL and Category image using Category-Id?
As we know that in the Category Management page we can upload an image and give a URL for each category. I want to retrieve the Category name, Category URL and Category image using only the Category Id that I already have with me. Is it possible to do so?
Filter magento product collection by category id but NOT category children
I am retrieving a product collection and filtering the list by category. I have just had to add some sub categories to one of them and now the products in the sub-categories are appearing in the collection for the parent category and I cannot work out how to only show products directly assigned to the parent category. Here is an example of the code...
Filter Product Collection By Another Attribute In Product Collection eg 'attribX', array('gt' => 'attrib-Y')
When using product collections like so: Is it possible to use addAttributeToFilter on an attribute relative to another attribute? e.g. Can I filter special_price greater than price with something like
Reducing page load time on product collection / product collection usage options
I want to show the full product list from a category without pagination, however with ~500 simple products page loading is slow. Is there any way I can fetch the full collection and show sections at a time, reducing page load times? Alternatively is there a way I can setPageSize(20) (or something), but save the full collection for use later in the ...
Magento 2 override the product collection(Magento\Catalog\Model\ResourceModel\Product\Collection.php)
We want to override the product collection model class(Magento\Catalog\Model\ResourceModel\Product\Collection.php) We flow the below process to override the addAttributeToSort method but not working. di.xml Collection.php Please, can you help on above issue? any references and suggestion are highly appreciated.

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.