Wednesday, 20 August 2014

how to get products by category id in magento

<?php
/*get all product by category*/

     foreach ($cat_id as $cat_ids)//for each run by category id
{
   
$catagory_model = Mage::getModel('catalog/category')->load($cat_ids);
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addCategoryFilter($catagory_model);
$collection->addAttributeToFilter('status',1);
$collection->addAttributeToSelect(array('name','id','url','small_image')); //get product field
$collection->addStoreFilter();         
if(!empty($collection))
{
foreach ($collection as $_product):?>

        <a href="<?php echo $_product->getProductUrl();?>"><img src="<?php echo Mage::helper('catalog/image')->init($_product, 'small_image')->resize(197, 167); ?>" />
        <span><?php echo $_product->getId();?></span><span><?php echo $_product->getName();?></span>  </a> 
<?php $pr_id= $_product->getId();?>

<?php array_push($pro_id, $pr_id ); ?>//push all product id in array.
 <?php   endforeach;

}else
    {
echo 'No products exists';

    }           
 ?> 

No comments:

Post a Comment