Possible Duplicate:
How to get products from a particular category in magento ecommerce
I want to build a template (.phtml file) that displays a grid of products (name, description price, small image with link) for Magento 1.7, similar to the default “New Product” block. Here is what I have so far…
<?php
$category=Mage::getModel('catalog/category')->load(4);
$category=$category->getProductCollection();
foreach($category->getAllIds() as $id)
{
$product=Mage::getModel('catalog/product')->load($id);
echo $product->getName()."<br/>";
echo $product->getUrl()."<br/>";
echo $product->getImageUrl()."<br/>";
echo $product->getImageDescription()."<br/><br/>";
}
Your code has a couple of mistakes in it, let me highlight them:
$category=$category->getProductCollection();– this is a very bad practice. You’re assigning the Collection of product to a variable that was previously a category. It’s easy to get lost in such code. Please remember to use meaningful naming conventions for your variable. This like should be$productCollection=$category->getProductCollection();Instead of looping the collection that you already have, you’re getting the array of ids, and looping through them, loading objects during each loop. This will lead to memory leaks and very large amount of time your code will perform. The
foreachstatement should be like this:foreach ($productCollection as $product).You haven’t actually asked a question here. You’ve said that you’re going to make grid, and posted your code, but where’s the problem?
If you want to create Product Grid, please review how it’s done in Magento core Catalog module : block
Mage_Catalog_Block_Product_Listand template catalog/product/list.phtml