Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Page 1 of 3
Custom Best Sellers Module - Finished! 
 
bloggleme
Sr. Member
 
Avatar
Total Posts:  101
Joined:  2008-05-06
 

Alright, I looked around on the forums for a way to list the best sellers (by actual product sales). This is a workaround and should in no way at all be the permanent solution.

First, follow the tutorial for listing featured products on the home page. (dan_w’s tutorial)

1. Do the same thing you would for a featured block, but make it a bestselling block. Heres what needs changed initially:
Homepage.php (change to Bestsellers.php)

class Mage_Catalog_Block_Product_Homepage extends Mage_Catalog_Block_Product_Abstract
change this to:
class Mage_Catalog_Block_Product_Bestsellers extends Mage_Catalog_Block_Product_Abstract

2. Change homepage.phtml to bestselling.phtml

3. Add your block to the correct page:

{{block type="catalog/product_bestsellers" name="home.catalog.product.bestsellers" alias="product_bestsellers" template="catalog/product/bestsellers.phtml"}}

4. Make an attribute named sales_rank (text area) and add it to your default attribute set.

Now that you have everything set up, I feel I should warn you: The best selling script will make your pages load slow. I suggest making it a cronjob and run it nightly instead of running it at every page load. Basically, this script goes through all of the orders, checks the items, talys the product sales, assigns sales to an array of productIDs, and then loads products based on that ID. After that, a custom attribute is set (sales_rank) and the product is saved. Then we use our sales_rank to sort the products.

$SalesPerProduct = array();
        
$i=1;
        while(!isset(
$End)){

            $order 
Mage::getModel('sales/order')->load($i);
            
$items $order->getAllItems();
            if(empty(
$items)){$End='now';}
            
//$ids = array_keys($items);
            //$skus = array();
            
foreach ($items as $itemId => $item{
                $itemId 
$item->getProductId();
                if(!isset(
$SalesPerProduct[$itemId])) {
                    $SalesPerProduct[$itemId] 
0;
                
}
                $SalesPerProduct[$itemId]
++;
            
}
            $i
++;

        
}
        arsort
($SalesPerProduct);
        
        
        foreach (
$SalesPerProduct as $ProductID => $Sales{
            $ProductForUpdate 
Mage::getModel('catalog/product')->load($ProductID);
            
$ProductForUpdate->setSalesRank($Sales);
            
$ProductForUpdate->save();
            
//print_r($ProductForUpdate);
        
}
        
/*END*/

This is the code that updates all of the products. Put it where you like - but I suggest making it a nightly cronjob (so your best sellers will only update daily, but this isn’t a big deal).

Next, delete this line in Bestselling.php:

$products->setOrder('hot_deals')->setPageSize(5)->setCurPage(1);

Now, find this code:

$products   $product->setStoreId($storeId)->getCollection()
            ->
addAttributeToSelect(array('name''price''small_image'), 'inner')
            ->
addAttributeToSelect(array('special_price''special_from_date''special_to_date'), 'left')
        ;
And add an extra line to it, as so:
$products   $product->setStoreId($storeId)->getCollection()
            ->
addAttributeToSelect(array('name''price''small_image'), 'inner')
            ->
addAttributeToSelect(array('special_price''special_from_date''special_to_date'), 'left')
            ->
setOrder('sales_rank''DESC')
        ;

This was a spur of the moment thing and I am sure there is a better way. Please comment back with suggestions!

 
Magento Community Magento Community
Magento Community
Magento Community
 
laurent
Member
 
Total Posts:  75
Joined:  2007-08-31
Paris, France
 

I’ll look at your code when I have time.
Here is mine. It’s a mix of frontend new products class and backend bestsellers reports!

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('*')
            ->
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);
    
}
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
bloggleme
Sr. Member
 
Avatar
Total Posts:  101
Joined:  2008-05-06
 

Your code seems to be much better as it wouldn’t put as much load on the server.

Question though, what is this

addOrderedQty()
and what does it do? Wheres the code for it?
 
Magento Community Magento Community
Magento Community
Magento Community
 
laurent
Member
 
Total Posts:  75
Joined:  2007-08-31
Paris, France
 
bloggleme - 28 May 2008 11:44 AM

Question though, what is this

addOrderedQty()

Thanks for pointing this out! I forgot to remove it from the admin code I took. There is no need for a quantity in the frontend.

UPDATE:
This code is needed. It gets the ordered quantities needed for ordering the products ->setOrder(’ordered_qty’, ‘desc’).
@see app/code/core/Mage/Reports/Model/Mysql4/Product/Collection.php

 
Magento Community Magento Community
Magento Community
Magento Community
 
bloggleme
Sr. Member
 
Avatar
Total Posts:  101
Joined:  2008-05-06
 

Very cool. I hadn’t realized that there was actually an ‘ordered_qty’ field to sort by.

 
Magento Community Magento Community
Magento Community
Magento Community
 
lotusseedsD
Mentor
 
Avatar
Total Posts:  1144
Joined:  2007-08-31
 
laurent - 28 May 2008 10:14 AM

I’ll look at your code when I have time.
Here is mine. It’s a mix of frontend new products class and backend bestsellers reports!

Hi Laurent, do you create a new ‘bestsellers.php’?

@bloggleme, if I use laurent’s code, the rest of the steps (starts from #4) are similar?

 
Magento Community Magento Community
Magento Community
Magento Community
 
bloggleme
Sr. Member
 
Avatar
Total Posts:  101
Joined:  2008-05-06
 

yup smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
laurent
Member
 
Total Posts:  75
Joined:  2007-08-31
Paris, France
 
chinesedream -

do you create a new ‘bestsellers.php’?

Yes, you have to call it Bestseller.php (because the class name is Mage_Catalog_Block_Product_Bestseller) and you can put it in app/code/local/Mage/Catalog/Block/Product/ to separate your custom code from the core code.

 
Magento Community Magento Community
Magento Community
Magento Community
 
lotusseedsD
Mentor
 
Avatar
Total Posts:  1144
Joined:  2007-08-31
 

Thanks both. I am going to try it out soon.  ‘The best selling script will make your pages load slow.’ has given me a second thought because right now Magento speed is still a concern.

I was just thinking when read through your message yesterday, that perhaps having a ‘bestseller’ attribute is another option (?), so that it’s sortable from the layered nav, and maybe it will not be an issue for page loads?

Update:
Got it working! and it loads pretty fast, maybe it’s because middle of the night that traffic is empty. smile

Thank you both for the module smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
bloggleme
Sr. Member
 
Avatar
Total Posts:  101
Joined:  2008-05-06
 

Yea, laurent’s will not load slow - it is definitely the one you should use smile however, hers did give me an idea and I’m going to release a module that grabs the top rated (based on product star ratings).

 
Magento Community Magento Community
Magento Community
Magento Community
 
lotusseedsD
Mentor
 
Avatar
Total Posts:  1144
Joined:  2007-08-31
 

bloogleme, with laurent’s code, I don’t see the need for ‘sales_rank’ attribute, however I am curious if it was something you specifically had it in mind.

thanks!

 
Magento Community Magento Community
Magento Community
Magento Community
 
bloggleme
Sr. Member
 
Avatar
Total Posts:  101
Joined:  2008-05-06
 

chinesedream,

you can forget about the sales rank completely. Just replace the BestSelling.php code with what laurent has.

 
Magento Community Magento Community
Magento Community
Magento Community
 
jimmypalmer
Sr. Member
 
Total Posts:  79
Joined:  2008-05-04
 

doesn’t seem to work for configurable products.  Only simple products.  Or am I doing something wrong?

 
Magento Community Magento Community
Magento Community
Magento Community
 
lotusseedsD
Mentor
 
Avatar
Total Posts:  1144
Joined:  2007-08-31
 
jimmypalmer - 30 May 2008 12:45 PM

doesn’t seem to work for configurable products.  Only simple products.  Or am I doing something wrong?

Thanks for the heads up. Yeah, just tested, it’s not showing up.

 
Magento Community Magento Community
Magento Community
Magento Community
 
bloggleme
Sr. Member
 
Avatar
Total Posts:  101
Joined:  2008-05-06
 

Hmm I haven’t tested it with configurable products.  :/

 
Magento Community Magento Community
Magento Community
Magento Community
 
coug
Jr. Member
 
Total Posts:  20
Joined:  2008-06-27
 

Hello,

I’m trying to put a link to the category of each best sellers product, but i don’t know how get the category name and url.

If someone have made it or can help me.

Thanks.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 3