Sunday, July 15, 2012

Magento: Show all the Products from specific category

Here we display all the products from specific category in Magento. Below code needs category ID and product collection Object needed.  Please use the below code to show all the products using category ID.

<?php
$category_id = 10; // if you know static category then enter number

    $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';

    }              

?>


In the above code to display all the product names from specific category. If you want display all the product information, under the foreach loop need to use belo informations

// get Product's name
echo $_product->getName();
//get product's short description
echo $_product->getShortDescription();
//get Product's Long Description
echo $_product->getDescription();
//get Product's Regular Price
echo $_product->getPrice();
//get Product's Special price
echo $_product->getSpecialPrice();
//get Product's Url
echo $_product->getProductUrl();
//get Product's image Url
echo $_product->getImageUrl();


Hope Magento product lists code is useful for you. Kindly update your comments. Thanks for sharing this.

By PHP with 1 comment

1 comments:

Post a Comment

    • Popular
    • Categories
    • Archives