Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

List of products in particular category
 
tin_soldier
Jr. Member
 
Total Posts:  26
Joined:  2008-08-29
 

Hi,

I’m trying to create a product list (name and link of products only) from a particular category. I want to display this on all pages.

So far I’ve put this into catalog.xml in the default section:

<reference name="header">
        <
block type="catalog/product_list" name="category.productlist" >
                <
action method="setTemplate"><template>catalog/product/list_new.phtml</template></action>
        </
block
</
reference>

and have made list_new.html in template>catalog>product with the following code which I’ve patched from a few suggestions on the forum:

<?
Mage
::getSingleton('catalog/layer')->setData("product_collection",NULL);
$_productCollection=$this->getLoadedProductCollection()
?>
<?
$cat_id 
4// category_id for "specific category"
$_productCollection Mage::getResourceModel('catalog/product_collection')
->
addCategoryFilter(Mage::getModel('catalog/category')->load($cat_id)); 
?>

<?if
(!$_productCollection->getSize()):?>
<div class="note-msg">
    
<?=__('There are no products matching the selection.')?>
</div>
<?else:?>
    
<ul>
    
<?foreach ($_productCollection as $_product):?>
        
<li><a href="<?=$_product->getProductUrl()?>" title="<?=$this->htmlEscape($_product->getName())?>"><?=$_product->getName()?></a></li>
    
<?endforeach;?>
    
</ul>
<?endif;?>

This detects correctly whether there are products for the chosen category or not and lays out the unordered list but is not retrieving the products names or urls.

If I remove:

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

Then it gives me the list I want but only if I’m already at the landing page for that category.

Can anyone tell me what I have wrong here?

Thanks.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top