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).
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();
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');
->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);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
//$products->setPageSize(6)->setCurPage(1);
$this->setProductCollection($products);
}
}
?>
}
}
?>
SpecificCategory.php
01.<?php02.//Grab products from a specific category - in this case category 1303.class Mage_Catalog_Block_Product_Bookclub extendsMage_Catalog_Block_Product_Abstract04.{05.public $_collection;06. 07.protected function _getProductCollection()08.{09.$storeId = Mage::app()->getStore()->getId();10.$product = Mage::getModel('catalog/product');11.$category = Mage::getModel('catalog/category')->load(13);//category whos ID is 1312. 13.$visibility = array(14.Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,15.Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG16.);17. 18.$products = $product->setStoreId($storeId)19.->getCollection()20.->addAttributeToFilter('visibility', $visibility)21.->addCategoryFilter($category)22.->addAttributeToSelect(array('name', 'price', 'small_image','short_description', 'description', 'author'), 'inner')//example custom attributes23.->setOrder('created_at', 'desc')24.->addAttributeToSelect(array('special_price','special_from_date', 'special_to_date'), 'left')25.;26. 27.$this->_collection = $products;28.return $this->_collection;29. 30.}31. 32.public function getCurrentCategory() {33.return Mage::getModel('catalog/category')->load(13);34.}35. 36.public function getProductCollection() {37.return $this->_getProductCollection();38.}39.}40.?>SpecificCategoryLayer.php
01.<?php02./*This is the same as above but grabs the product collection from the 'catalog/layer' singleton.03.I've had to use this in some pre-1.1.x Magento builds because the above code version created some04.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.05. 06.Note on use: You can only set the category of the layer once per page request, so you CANNOT use07.this block of code multiple times on one page unless you want to repeat products from the same08.category multiple times on one page.09.*/10.class Mage_Catalog_Block_Product_Bookclub extendsMage_Catalog_Block_Product_Abstract11.{12.public $_collection;13. 14.protected function _getProductCollection()15.{16.$storeId = Mage::app()->getStore()->getId();17.$product = Mage::getModel('catalog/product');18.$layer = Mage::getSingleton('catalog/layer');19. 20.$category = Mage::getModel('catalog/category')->load(13);//category by ID21. 22.$layer->setCurrentCategory($category);23.$visibility = array(24.Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,25.Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG26.);27. 28.$products = $layer->getProductCollection()29.->addAttributeToSelect('author', 'inner') //example custom attributes30.->addAttributeToSelect('shelf_talker', 'inner')31.->setOrder('created_at', 'desc');32. 33.$this->_collection = $products;34.return $this->_collection;35. 36.}37. 38.public function getCurrentCategory() {39.return Mage::getModel('catalog/category')->load(13);40.}41. 42.public function getProductCollection() {43.return $this->_getProductCollection();44.}45.}46.?>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
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).
No comments:
Post a Comment