Saturday, September 21, 2013

Get products by category id in Magento

It is always recommended to fetch any data from mangeto using its api/helper.

Here the code to get list of products by passing category id with the help of magento helper.

if it is current category,

$category = Mage::registry('current_category');

If not,

$categoryid = 12;
$category = new Mage_Catalog_Model_Category();
$category->load($categoryid);

Then,

$items = $category->getProductCollection();
$items->addAttributeToSelect('*');

$p_ids = array();

foreach($items as $item)
{
  $p_ids[] = $item->getID();
}

No comments:

Post a Comment