Some custom Blocks to help you show products

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
class Mage_Catalog_Block_Product_Bestseller extends Mage_Catalog_Block_Product_Abstract
{
public function __construct()
{
parent::__construct();

$storeId = Mage::app()->getStore()->getId();

$products = Mage::getResourceModel(’reports/product_collection’)
->addOrderedQty()
->addAttributeToSelect(array(’name’, ‘price’, ’small_image’, ’short_description’, ‘description’, ‘author’))
->setStoreId($storeId)
->addStoreFilter($storeId)
->setOrder(’ordered_qty’, ‘desc’);

Mage::getSingleton(’catalog/product_status’)->addVisibleFilterToCollection($products);
Mage::getSingleton(’catalog/product_visibility’)->addVisibleInCatalogFilterToCollection($products);

//$products->setPageSize(6)->setCurPage(1);

$this->setProductCollection($products);
}
}
?>

SpecificCategory.php
<?php
//Grab products from a specific category – in this case category 13
class Mage_Catalog_Block_Product_Bookclub extends Mage_Catalog_Block_Product_Abstract
{
public $_collection;

protected function _getProductCollection()
{
$storeId = Mage::app()->getStore()->getId();
$product = Mage::getModel(‘catalog/product’);
$category = Mage::getModel(‘catalog/category’)->load(13); //category whos ID is 13

$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’, ‘price’, ‘small_image’, ‘short_description’, ‘description’, ‘author’), ‘inner’) //example custom attributes
->setOrder(‘created_at’, ‘desc’)
->addAttributeToSelect(array(‘special_price’, ‘special_from_date’, ‘special_to_date’), ‘left’)
;

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

}

public function getCurrentCategory() {
return Mage::getModel(‘catalog/category’)->load(13);
}

public function getProductCollection() {
return $this->_getProductCollection();
}
}
?>
SpecificCategoryLayer.php
<?php
/*This is the same as above but grabs the product collection from the ‘catalog/layer’ singleton.
I’ve had to use this in some pre-1.1.x Magento builds because the above code version created some
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.

Note on use: You can only set the category of the layer once per page request, so you CANNOT use
this block of code multiple times on one page unless you want to repeat products from the same
category multiple times on one page.
*/
class Mage_Catalog_Block_Product_Bookclub extends Mage_Catalog_Block_Product_Abstract
{
public $_collection;

protected function _getProductCollection()
{
$storeId = Mage::app()->getStore()->getId();
$product = Mage::getModel(‘catalog/product’);
$layer = Mage::getSingleton(‘catalog/layer’);

$category = Mage::getModel(‘catalog/category’)->load(13); //category by ID

$layer->setCurrentCategory($category);
$visibility = array(
Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
);

$products = $layer->getProductCollection()
->addAttributeToSelect(‘author’, ‘inner’) //example custom attributes
->addAttributeToSelect(‘shelf_talker’, ‘inner’)
->setOrder(‘created_at’, ‘desc’);

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

}

public function getCurrentCategory() {
return Mage::getModel(‘catalog/category’)->load(13);
}

public function getProductCollection() {
return $this->_getProductCollection();
}
}
?>
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

Your email address will not be published. Required fields are marked *