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
anyone tell me where i add this code?
This is an independent script and can be used on any page, you will only need category id for this to work.
The blog is absolutely fantastic. Lots of great information and inspiration, both of which we all need. Thanks.
Script is correct for get product collection by single category id.
Thanks a lot.
Good Job Zahid!! Keep it up
Hello this is a very helpfull posti love that its solve my problem
Hi,
I’m new to Magento, how do you implement this code.
Regards
Alejandro.
@Alejandro – Please go through this link for better understanding http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-1-introduction-to-magento
Oh,Thank you very much!you save me time!!other website get products by category id solution is no use for me,but you are userful to me.
@Deven – I am glad it was useful to you.
Sir
Your blog Really help me
Thanks For your support
@Suman – I am glad it was helpful to you
Hi,
Really This Very help full.
Thanks very nice blog….
🙂
Hi,
Really This Very help full.
Funtastic job.
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”.
@Hassan – I am glad it was useful to you 🙂
How to filter color in you example?
@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