How to get products from a particular category in magento ecommerce

Please LogIn to Reply!

Post By: admin | 26-01-2012 06:35 Reply

I'd like to get a list of random products from the same category as the current product for displaying within the product view - so far all I've dug up is

Does anyone know how to do this?

what I ended up doing is in app/design/frontend/default/theme_name/template/catalog/product/list_random.phtml

doing something like:


$_category = $this->getCurrentCategory();
$subs = $_category->getAllChildren(true);
$result = array();
foreach($subs as $cat_id) {
    $category = new Mage_Catalog_Model_Category();
    $collection = $category->getProductCollection();
    foreach ($collection as $product) {
        $result[] = $product->getId();


this will get you an array of product id's. You can loop through them and create products on the fly using:

foreach ($result as $_product_id){ 
    $_product = new Mage_Catalog_Model_Product();
    //do something with the product here

then, create a static block in the cms with the following content

{{block type="catalog/navigation" template="catalog/product/list_random.phtml"}}

Finally, in the Catalog->Manage categories section, choose the category, then the display settings tab. Switch the display mode to "Static block and products" and then choose your block from the drop list.

And that should do it.

Here is the code to get products from any particular category:-

$productCollection = Mage::getResourceModel('catalog/product_collection')

Here is a blog entry from my website that explains how to get products from a category:

You should instantiate a model by calling Mage::getModel('catalog/product') in this case.because then you get a configured object instance, extended by any configured modules. If you do it like 'new Mage_Catalog_Model_Product()' this will ignore modules and bypass the magento api.


`Reply Or Comment!
Please LogIn to Reply!