Terry - 专注外贸B2C


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

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 exploremagento.com, 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.

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/terry_water/article/details/6157702
下一篇Show quantity, total price and shipping price in my header.phtml?
想对作者说点什么? 我来说一句

Learning Jupyter

2017年12月06日 9MB 下载