Magento: How to list all the products in a single category in a sidebar block

原创 2011年01月21日 21:09:00

Magento: How to list all the products in a single category in a sidebar block
2009-10-25 21:54

03.23.2009 05:57PM by Evan Johnson

The website I’m currently developing wants to have all of the products in “CategoryX” displaying in the left sidebar on all pages. It’s a small category with their 5 core products, and they want to make sure no matter where the visitor is on the site, they are just a click away from viewing these products. The sidebar block only consists of a header with the Category name, and a simple list of the products with links to their product information page. It’s just links in this case – no images or prices or “Add to Cart” buttons – but you could add these easily.

I tried to simply use the “Catalog/Product_List” controller with a modified view based off of “catalog/product/list.phtml”. The problem I ran into is that “Catalog/Product_List” uses the session’s current Category/Layer to populate the list, so I couldn’t show the products in CategoryX in the sidebar when I was viewing CategoryY. I read that you can specify which category it uses, but this only seems to work on pages without a Category/Layer, like the Home/Front page and similar CMS pages.

Fortunately I found a nice snippet of code on, which gave me a clue about how to do this. I’ve modified this code to better suit my needs, and make it a little more general at the same time. You will need to know how to create a simple Magento module to implement this, and there is a good guide here.

Anyway, here it is the code:

class Mycompany_ProdList_Block_Left extends Mage_Catalog_Block_Product_Abstract  

public $_collection;
public $_catName;
private $_theCat;

public function setCat($category) {
$this->_theCat = $category;

public function getCatName() {
return $this->_catName;

protected function _getProductCollection() {
$storeId = Mage::app()->getStore()->getId();
$product = Mage::getModel(‘catalog/product’);
$category = Mage::getModel(‘catalog/category’)->load($this->_theCat);
$this->_catName = $category->getName();

$visibility = array(

$products = $product->setStoreId($storeId)
->addAttributeToFilter(‘visibility’, $visibility)
->addAttributeToSelect(array(‘name’), ‘inner’) //you need to select “name” or you won’t be able to call getName() on the product
->setOrder(‘name’, ‘asc’)

$this->_collection = $products;
return $this->_collection;

public function getCurrentCategory() {
return Mage::getModel(‘catalog/category’)->load($this->_theCat);

public function getProductCollection() {
return $this->_getProductCollection();

And this is how you pass in the category you want to show (in this case, category ID 4), via Layout XML:

<block type="mycompany_prodlist/left" name="leftprodlist" template="prodlist/left.phtml" />
<reference name="leftprodlist">
<action method="setCat"><category>4</category></action>

And finally here is the template file I used to display it (prodlist/left.phtml):

if(!$this->_collection->count()): ?>
<div class="note-msg">
<?php echo $this->__('There are no products matching the selection.') ?>
<?php else: ?>
<div class="box base-mini mini-list">
<div class="head">
<h4><?php echo $this->getCatName(); ?></h4>
<?php //echo Mage::registry('current_category')->getId(); ?>
<?php foreach ($this->_collection as $_product): ?>
<li><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a></li>
<?php endforeach; ?>
<?php endif; ?>

The “_product” model returned is similar to the one used by Catalog/Product_List, so you can work from that example to give your list images, prices, etc. One thing to note though: it looks like you might need to “select” all the variables you’re going use. For instance, the call $_product->getName() didn’t work unless I called addAttributeToSelect(array(‘name’), ‘inner’) when getting the product collection.


CC150 3.1 Three in one: describe how you could use a single array to implement 3 stacks.

fixed Division思路: 1 把数组等分; 2 允许stack在其中增长; stack0 [0,n/3) stack1 [n/3,2n/3) stack2 [2n/3,...

how to block call in CUCM

  • 2014年07月22日 14:05
  • 14KB
  • 下载

How to create coupon codes in Magento – Beginner’s Tutorial

Want to set up a promotion on your Magento eCommerce store? Would you like to give your users free...
  • fuyifa
  • fuyifa
  • 2016年05月12日 17:25
  • 442

Magento. How to create an attribute and apply it to products

This tutorial is going to show you how to create an attribute and apply it to products on your Magen...
  • fuyifa
  • fuyifa
  • 2016年05月10日 13:57
  • 826

How to identify all the Corrupted Objects in the Database reported by RMAN

Step 1: Identify the corrupt blocks Populate the v$database_block_corruption view with  information...

How to add custom fields to Customer Registration and Account page in magento

经过测试,一下的代码在magento CE1.4 1.5 1.6 均好使 Magento Provides the default customer registration form with...

About UID and How to autostart an application on boot up in 3rd- Startup List Management API

Overview UID of self-signed applications Description S60 3rd Edition introduces mandatory signing fo...
  • cyforce
  • cyforce
  • 2011年04月04日 00:11
  • 815

How to list processes attached to a shared memory segment in linux?

  • bonny95
  • bonny95
  • 2011年05月13日 15:16
  • 806

Is appending block by handle possible in Magento layout xml?

 Is appending block by handle possible in Magento layout xml?Posted onFebruary 17, 2011by芳草苑主In the...

How To List All The Named Events Set For A Database

How To List All The Named Events Set For A Database [ID 436036.1] Applies to: Oracle Server - E...
您举报文章:Magento: How to list all the products in a single category in a sidebar block