search
top

Specific Category Products Magento

Hello All,

I am writing here the script to get product collection of a required category.

For the below mentioned script we must have the category id for which the product collection is needed.

<?php

	$catagory_model = Mage::getModel('catalog/category')->load($category_id); //where $category_id is the id of the category

	$collection = Mage::getResourceModel('catalog/product_collection');

	$collection->addCategoryFilter($catagory_model); //category filter

	$collection->addAttributeToFilter('status',1); //only enabled product

	$collection->addAttributeToSelect(array('name','url','small_image')); //add product attribute to be fetched

	//$collection->getSelect()->order('rand()'); //uncomment to get products in random order	    

	$collection->addStoreFilter();     	    

	if(!empty($collection))

	{

    		foreach ($collection as $_product): 

			echo $_product->getName();	//get product name			

		endforeach;

	}else 

    	{

    		echo 'No products exists';

	}       	    

?>

Hope this is helpful



6 Responses to “Specific Category Products Magento”

  1. Rakes says:

    Thanks very nice blog….

    :)

  2. Jitendra says:

    Hi,
    Really This Very help full.

    Funtastic job.

  3. Hassan Ali SHahzad says:

    This is one that I am searching from last two days.
    please change heading from “Get Category Products Magento” to “Get Product data for the specific Category”.

  4. Zahid says:

    How to filter color in you example?

    • DWRoot says:

      @Zahid – You need to add the attribute code (suppose it is ‘color’)like ‘name’ in $collection->addAttributeToSelect(array(‘name’,'url’,'small_image’,'color’)); and use ‘$collection->addAttributeToFilter(‘color’,array(‘eq’=>’green’)); ‘ for filtering.

      Hope this is helpful

Leave a Reply

top