Magento Themes and Magento Extensions

Some custom Blocks to help you show products

Share

I have a few custom blocks I’ve written / copied and tweaked from various posts on Magento’s forums. I noticed they are pretty universal in how they grab, filter and return a product collection to be used in various template files (.phtml files). The blocks below should all work from List.phtml (app\design\frontend\default\default\template\catalog\product\list.phtml).

These are Block files and are most appropriately used within the Catalog/Product area. (See my post on creating a custom module – these files won’t be overwriting any other class but can stand alone).

Be aware that the code below contains code for custom attributes that I happened to use on a project. They most likely will not be the same your particular instances. (the Bestseller block should not be affected by this issue).

Bestseller.php

//bestseller module – grabs from all products, returns in order of total quantity ordered

01.class Mage_Catalog_Block_Product_Bestseller extends
02.Mage_Catalog_Block_Product_Abstract
03.{
04.public function
05.__construct()
06.{
07.parent::__construct();
08.
09.$storeId = Mage::app()->getStore()->getId();
10.
11.$products =
12.Mage::getResourceModel(’reports/product_collection’)
13.->addOrderedQty()
14.->addAttributeToSelect(array(’name’,
15.‘price’, ’small_image’, ’short_description’, ‘description’,
16.‘author’))
17.->setStoreId($storeId)
18.->addStoreFilter($storeId)
19.->setOrder(’ordered_qty’,
20.‘desc’);
21.
22.Mage::getSingleton(’catalog/product_status’)->addVisibleFilterToCollection($products);
23.Mage::getSingleton(’catalog/product_visibility’)->addVisibleInCatalogFilterToCollection($products);
24.
25.//$products->setPageSize(6)->setCurPage(1);
26.
27.$this->setProductCollection($products);
28.}
29.}
30.?>

SpecificCategory.php

01.</pre>
02.</div>
03.<div class="syntaxhighlighter">
04.<pre><?php
05.//Grab products from a specific category - in this case category 13
06.class Mage_Catalog_Block_Product_Bookclub extendsMage_Catalog_Block_Product_Abstract
07.{
08.public $_collection;
09.
10.protected function _getProductCollection()
11.{
12.$storeId = Mage::app()->getStore()->getId();
13.$product = Mage::getModel('catalog/product');
14.$category = Mage::getModel('catalog/category')->load(13);//category whos ID is 13
15.
16.$visibility = array(
17.Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
18.Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
19.);
20.
21.$products = $product->setStoreId($storeId)
22.->getCollection()
23.->addAttributeToFilter('visibility', $visibility)
24.->addCategoryFilter($category)
25.->addAttributeToSelect(array('name', 'price','small_image', 'short_description', 'description','author'), 'inner') //example custom attributes
26.->setOrder('created_at', 'desc')
27.->addAttributeToSelect(array('special_price','special_from_date', 'special_to_date'), 'left')
28.;
29.
30.$this->_collection = $products;
31.return $this->_collection;
32.
33.}
34.
35.public function getCurrentCategory() {
36.return Mage::getModel('catalog/category')->load(13);
37.}
38.
39.public function getProductCollection() {
40.return $this->_getProductCollection();
41.}
42.}
43.?></pre>

SpecificCategoryLayer.php

01.</pre>
02.</div>
03.<div class="syntaxhighlighter">
04.<pre><?php
05./*This is the same as above but grabs the product collection from the 'catalog/layer' singleton.
06.I've had to use this in some pre-1.1.x Magento builds because the above code version created some
07.weird errors when users edited any product in the backend (resulting in front end server time outs). That error was probably local to that Magento build, but it resulted in this code.
08.
09.Note on use: You can only set the category of the layer once per page request, so you CANNOT use
10.this block of code multiple times on one page unless you want to repeat products from the same
11.category multiple times on one page.
12.*/
13.class Mage_Catalog_Block_Product_Bookclub extendsMage_Catalog_Block_Product_Abstract
14.{
15.public $_collection;
16.
17.protected function _getProductCollection()
18.{
19.$storeId = Mage::app()->getStore()->getId();
20.$product = Mage::getModel('catalog/product');
21.$layer = Mage::getSingleton('catalog/layer');
22.
23.$category = Mage::getModel('catalog/category')->load(13);//category by ID
24.
25.$layer->setCurrentCategory($category);
26.$visibility = array(
27.Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
28.Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
29.);
30.
31.$products = $layer->getProductCollection()
32.->addAttributeToSelect('author', 'inner') //example custom attributes
33.->addAttributeToSelect('shelf_talker', 'inner')
34.->setOrder('created_at', 'desc');
35.
36.$this->_collection = $products;
37.return $this->_collection;
38.
39.}
40.
41.public function getCurrentCategory() {
42.return Mage::getModel('catalog/category')->load(13);
43.}
44.
45.public function getProductCollection() {
46.return $this->_getProductCollection();
47.}
48.}
49.?></pre>

These blocks do not represent all there is to making use of them. As mentioned, these should all work with “List.phtml” in the Catalog module design files, but you might want to create your own template.

The easiest way to implement these and make them usable is to add them to a Mage folder within the Local code folder (rather than core):
app/code/local/Mage/Catalog/Block/Product/Bestseller.php

This lets you skip the step of creating a custom module in order to use a simple block who’s only purpose is to list a category of products (or bestsellers in this case).

Leave a Reply

You must be logged in to post a comment.