|
Hi,
I would like to see my first work in the core because IMO peoble would like this very much!
THe following code shows the bestsellers (most sold products) of a chosen category at the product page (like the upsellers).
You can download the whole code at my website http://www.ringer.it/en/magento/code-snippets.html and here is it in short too
class Cyberhouse_Catalog_Block_Product_Bestseller extends Mage_Catalog_Block_Product_Abstract { protected $_columnCount = 4; protected $_items; protected $_itemCollection;
protected function _prepareData() { // get some Ids $storeId = Mage::app()->getStore()->getId(); $product = Mage::getModel('catalog/product'); // current category $category = Mage::registry('current_category'); if ($category) { // get all ordered products of this category, orded by ordered quantity $collection = Mage::getResourceModel('reports/product_collection') ->addOrderedQty() ->addAttributeToSelect('*') ->setStoreId($storeId) ->addStoreFilter($storeId) ->addCategoryFilter($category) ->setOrder('ordered_qty', 'desc') ->setPageSize(6) ->setCurPage(1) ; /* @var $products Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection */ Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection); Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection); $collection->load(); $this->_itemCollection = $collection; return $this; } return false; }
protected function _beforeToHtml() { $this->_prepareData(); return parent::_beforeToHtml(); }
public function getItemCollection() { return $this->_itemCollection; }
public function getItems() { if (is_null($this->_items)) { $this->_items = $this->getItemCollection()->getItems(); } return $this->_items; }
public function getRowCount() { return ceil($this->getItemCollection()->getSize()/$this->getColumnCount()); }
public function setColumnCount($columns) { if (intval($columns) > 0) { $this->_columnCount = intval($columns); } return $this; }
public function getColumnCount() { return $this->_columnCount; }
public function resetItemsIterator() { $this->getItems(); reset($this->_items); }
public function getIterableItem() { $item = current($this->_items); next($this->_items); return $item; } }
It is my first work with Magento, so be nice to me
If something can be improved, please tell me!
Georg
PS: Please use the version from my website because I will do updates there, a readme.txt is included
|