Call-back icon  Sales: Call 800.374.8146 (N America)|757.278.0920 (International)

Magento

Open Source eCommerce Evolved

Magento Forum

   
Categorybased Bestsellers on product page [Already coded!!]
 
Georg Ringer
Jr. Member
 
Avatar
Total Posts:  24
Joined:  2008-07-04
Linz, Austria
 

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 wink
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

 
Magento Community Magento Community
Magento Community
Magento Community
 
norbolig
Sr. Member
 
Total Posts:  272
Joined:  2008-03-15
 

1+. It would be nice to have this as an extention.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Georg Ringer
Jr. Member
 
Avatar
Total Posts:  24
Joined:  2008-07-04
Linz, Austria
 

I don’t know how to create those :( Is there any wiki page or similar?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Ron Seigel
Guru
 
Avatar
Total Posts:  389
Joined:  2007-08-31
Ottawa, ON
 

http://www.magentocommerce.com/wiki/how_to_use_magentoconnect

 Signature 

Ron
a1webShopping.com

Join the Drop Shipping group!

Need help deciding what to sell? OneSource - Millions of Products. Click Here!

 
Magento Community Magento Community
Magento Community
Magento Community
 
extremec
Jr. Member
 
Total Posts:  8
Joined:  2008-05-06
 

Thanks, this sounds like a great mod..

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top
 
Sales: Call 800.374.8146 (North America) 757.278.0920 (International)
© Copyright 2008 Varien. Magento is a trademark of Irubin Consulting Inc. DBA Varien
Privacy Policy|Terms of Service
Magento Community Count
33509 users|411 users currently online|74416 forum posts