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.


Display sub-category products in root category in Magento

父级目录不现实子目录的产品,这是Magento的默认做法,如果要让父级目录显示出子目录的产品,除了后台一个一个去勾选之外,还有更加简便的方法: Admin -> Catalog -> Manage ...
  • ddjohn
  • ddjohn
  • 2012年04月27日 17:16
  • 1053

PAT_A 1016. Phone Bills (25)

1016. Phone Bills (25) 排序 on-off
  • scylhy
  • scylhy
  • 2017年02月07日 17:30
  • 123

OCP-1Z0-051 第11题 sql运算顺序

一、原题 View the Exhibit and examine the structure of the PRODUCTS table. All products have a list pr...
  • hollo_hhy
  • hollo_hhy
  • 2014年04月27日 09:44
  • 3873

PAT--How Long Does It Take (25)

Given the relations of all the activities of a project, you are supposed to find the earliest comple...
  • grooowing
  • grooowing
  • 2015年04月22日 21:38
  • 1347

[LeetCode] Substring with Concatenation of All Words

Substring with Concatenation of All Words   You are given a string, S, and a list of words...
  • a83610312
  • a83610312
  • 2013年09月23日 11:33
  • 2125

[python] Adding or Deleting elements in a list or set

Adding elements in a list Deleting elements in a list Adding elements in a set Deleting elements in ...
  • 2015年12月26日 05:19
  • 262

1--magento 相关产品不显示 related product not show ---2---编译功能开启,但是后台出错上不去,编译的文件又出错的解决!!

1Solution: - Go to System -> Index Management - Reindex Data More detail over here: Mage...
  • terry_water
  • terry_water
  • 2011年03月05日 11:44
  • 1039

poj 1456 Supermarket (贪心, 并查集)

链接: 题目: Description A supermarket has a set Prod of pro...
  • shuangde800
  • shuangde800
  • 2012年09月27日 00:06
  • 2514

FRM-30425: Summarized database item must reside in a block with Query All Records or Precompute Summ

FRM-30425: Summarized database item must reside in a block with Query All Records or Precompute Summ...
  • SMJ3901080114
  • SMJ3901080114
  • 2013年05月14日 15:33
  • 4620

(2016秋数据结构课后练习题总结)03-树2 List Leaves (25分)

Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. ...
  • jiaomenglei
  • jiaomenglei
  • 2016年09月16日 21:26
  • 1188
您举报文章:Magento: How to list all the products in a single category in a sidebar block