0
votes

I am trying to display all products of a specific category. So i created a copy of /theme/template/catalog/list.phtml, and i added these couple of lines at the top:

$cat_id = 10; // category_id for "specific category"
$_productCollection = Mage::getResourceModel('catalog/product_collection')->addCategoryFilter(Mage::getModel('catalog/category')->load($cat_id));

Which replaced:

$_productCollection=$this->getLoadedProductCollection();

Now im getting all the products, but certain attributes and methods of the product dont seem to be available anymore($_product->isSaleable(), $this->getPriceHtml($_product) ). so the template does not show the product price or image correctly. It seems to me that certain helper functions are not available to me anymore since i referenced the model directly for the products, but i dont know enough about the Zend/Magento framework to know what method(s) to use instead.

I didnt paste the code for list.phtml, as this is just the default code for the base template on magento 1.3. Please let me know anyone needs this code for reference.

Thanks in advance!

2

2 Answers

2
votes

If you want to limit displaying of the products by a particular category, you need to set this category id into layer object (product list scope). It is not good practice to call models and initalize new collection inside of the template. You can simply set category id via layout and use old template code (without your modifications):

<reference name="your.custom.product.list">
    <action method="setCategoryId"><categoryId>10</categoryId></action>
</reference>
1
votes

Presumably because you are not specifying a store id nor scope, the collection doesn't know which attributes to load along with the product models. You can include that and shorten your code by doing this:

$cat_id = 10; // category_id for "specific category"
$_category = Mage::getModel('catalog/category')->load($cat_id);
$_productCollection = $_category->getProductCollection();