Magento: How to list all the products in a single category in a sidebar block
来源:互联网 发布:金融大数据刷新 编辑:程序博客网 时间:2024/05/16 19:57
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(
Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
);
$products = $product->setStoreId($storeId)
->getCollection()
->addAttributeToFilter(‘visibility’, $visibility)
->addCategoryFilter($category)
->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>
</reference>
And finally here is the template file I used to display it (prodlist/left.phtml):
<?php
$this->_getProductCollection();
if(!$this->_collection->count()): ?>
<div class="note-msg">
<?php echo $this->__('There are no products matching the selection.') ?>
</div>
<?php else: ?>
<div class="box base-mini mini-list">
<div class="head">
<h4><?php echo $this->getCatName(); ?></h4>
</div>
<?php //echo Mage::registry('current_category')->getId(); ?>
<ul>
<?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; ?>
</ul>
</div>
<?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.
- Magento: How to list all the products in a single category in a sidebar block
- How to Get All Product Images in products list page?
- How to Get All Product Images in products list page?
- Display sub-category products in root category in Magento
- 转:How to submit the rows of a repeating table in InfoPath to a SharePoint list
- How to enter a single quotation mark in Oracle?
- C#排序 How to Sort a List<T> by a property in the object
- How to convert a Map to List in Java?
- Find All Anagrams in a String Add to List
- Q.2.3 Implement an algorithm to delete a node in the middle of a single linked list, given only acce
- How to Get a List of Class Attributes in Python
- How to display column values in a single cell in Web Intelligence using Oracle database ?
- Bestseller products in Magento
- How to use the pointer of function in a class?
- How to Modify the Virtual Memory in a Mac
- How to unregister a receiver declared in the manifest
- How to remove the duplicated value in a String array
- How to Install a BTDownloader in the PI
- 代码技巧
- Visual C++ 2005 Redistributable Package不能安装的解决办法
- Magento Block的三种显示方式
- Apache负载均衡配置
- Magento迁移服务器server的整体步骤
- Magento: How to list all the products in a single category in a sidebar block
- Show quantity, total price and shipping price in my header.phtml?
- magento -- magento站点优化
- 求助,
- Advice for Computer Science College Students
- 如何在magento CMS homepage 加上breadcrumbs
- 【转】窗口style--WS_OVERLAPPEDWINDOW 分析
- SQL分组小计功能的实现
- 2011年1月21日