Sunday, 31 March 2013

Show all Products from specific category in Magento


Show all Products from specific category in Magento

 <?php
      
$category_id = Mage::getModel('catalog/layer')->getCurrentCategory()->getId(); // 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->addAttributeToFilter('visibility', 4); //only visibile product
$collection->addAttributeToSelect(array('name','url','small_image','thumbnail')); //add product attribute to be fetched
$collection->getSelect()->order('rand()'); //uncomment to get products in random order
$collection->getSelect()->limit(4); //set limit to display product
$collection->addStoreFilter();
if(!empty($collection))
{
foreach ($collection as $_product):
echo $_product->getName();   //get product name
endforeach;
} else
{
echo ‘No products exists’;
}
?>
And you also call other value related to product by using below function:-
// 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
<a href="<?php echo $_product->getProductUrl()?>" class="product-image"><img src="<?php echo Mage::helper('catalog/image')->init($_product, 'thumbnail')->resize(125, 125)?>" alt=" "></a>