Saturday 21 November 2015

To get product collection by category ID in Magento

Open your PHTML file of active template and use this code :
$categoryIds = array(Mage::registry('current_category')->getId());//category id


$_productCollection = Mage::getModel('catalog/product')
 ->getCollection()
 ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
 ->addAttributeToSelect('*')
 ->addAttributeToFilter('status','1')
 ->addAttributeToFilter('category_id', array('in' => $categoryIds));
?>
if(!$_productCollection->count()): 
     foreach ($_productCollection as $_product): 
       $product =  Mage::getModel('catalog/product')->load($_product->getId());
            $_product->getName() ;
    endforeach;
 endif; 

For all products of your current store :


$collection = Mage::getModel('catalog/product')->getCollection()->addStoreFilter($store_id); // default store Id is 1

   $collection ->setPageSize(6);

Note : if you want to get Products outside the magento, just call app/Mage.php file in your file, and use above codes.