Magento – get products from specific category

categorymagento-1.9

I need to get the products from a specific category and display in a specific template style.
I know you can do this on a cms page:

{{block type="catalog/product_list" name="product_list" category_id="209" column_count="4"  mode="grid" limit="25"  template="catalog/product/list.phtml"}}

But how can I do this in a php file?

Best Answer

You can do a loop through all products in a specific category and just pull back the info you need in the loop. Quick example:

<?php 

$catid=209;
$category = new Mage_Catalog_Model_Category();
$category->load($catid);

$prodCollection = $category->getProductCollection();


foreach ($prodCollection as $product):
  $product_id = $product->getId();
  $obj = Mage::getModel('catalog/product');
  $_product = $obj->load($product_id);

  //DISPLAY PRODUCT INFO HERE

  // 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();

  //ETC
endforeach;
?>

EDIT: Can you try this?

$this->getLayout()->createBlock('core/template')
->setData('category_id', '209')
->setData('column_count', '4')
->setData('mode', 'grid')
->setData('limit', '25')
->setData('name', 'product_list')
->setTemplate('catalog/product/list.phtml')->toHtml();
Related Topic