Tuesday, May 29, 2012

Magento: Best Selling Product Lists on Home Page


Bestseller or best selling product is one of the features people tend to ask for when it comes to Magento™.
There are multiple ways to implement this feature.
In this example, I’m not using controller or model directories at all; I’m going to show you how to implement this feature using only one file: the View.

Basically, what you need to do is to create the directory inside your template directory and place thebestseller.phtml file in it. In my example, I’m using the custom-created directory /inchoo. All of the screenshots provided here are based on that directory structure.
Adding this feature to your store is a matter of two simple steps:
  • copy bestseller-phtml file to your directory
  • display the block on home page
To add a block to a home page, you simply log into the Magento, CMS > Manage Pages > Home. Then add the following to the content area:
{{block type=”core/template” template=”inchoo/bestseller.phtml”}}
Notice the type attribute. I used core/template which means you can place this code anywhere in your site and it will work. Code does not inherit any collection objects from controllers since it has none. All that is necessary for the bestseller.phtml to work is defined in that single file.
One more thing: If you study the code in bestseller.phtml file you will see, at the very top of the file, the part that says: $this->show_total.
If I were to write
{{block type=”core/template” show_total=”12″ template=”inchoo/bestseller.phtml”}}
in my home page, then $this would be assigned property show_total with a value of 12.
Therefore, the provided bestseller.phtml file provides the option of setting the number of products you wish to see listed.
 
Here is the bestseller.phtml packed in bestseller.zip.


Method 2

Want to display the best selling products in your Magento store on the frontpage or anywhere else in your store? The best selling products means the products sold in highest quantity in Ascending order. This functionality is for some strange reason not included in Magento by default so we’ll explain how you can set it up yourself.
Main logic for this is we have to get the product list in ascending order by [order_qty]. So to get this type of list we have to apply
01$visibility array(
02Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
03Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
04);
05 
06$_productCollection = Mage::getResourceModel('reports/product_collection')
07->addAttributeToSelect('*')
08->addOrderedQty()
09->addAttributeToFilter('visibility'$visibility)
10->setOrder('ordered_qty''desc');
By this way you get the complete list of product order accoding to order quantity for particular product.
Now to display that list in well formated manner you have to loop over the array of product as
01<?php foreach($_productCollection as $product): ?>
02 
03<?php
04$categories = null;
05foreach (explode(","$product->category_ids) as $catId){
06 
07//Mage_Catalog_Model_Category
08$cat = Mage::getModel('catalog/category')
09->setStoreId(Mage::app()->getStore()->getId())
10->load($catId);
11$catName $cat->getName();
12 
13$catLink $cat->getUrlPath();
14$categories .= '<a href="'.$catLink.'" title="'.$catName.'">'.$catName.'</a>&nbsp;&nbsp;';
15}
16 
17?>
18 
19<?php if($counter <= $totalPerPage): ?>
20 
21<?php $productUrl =  $product->getProductUrl() ?>
22<div class="best-sellh">
23<div class="wraptocenter">
24<a href="<?php echo $productUrl ?>" title="View <?php echo $product->name ?>">
25<img src="<?php echo $this->helper('catalog/image')->init($product, 'image')->resize(120); ?>" alt="Product image"  class="shadow" rel="black" />
26<!--        <img src="images/prodimg01.jpg" alt="chrysler-building" height="150"width="100" class="shadow" rel="black"/>-->
27</a>
28</div>
29 
30<div class="img_txt" >
31<span class="yellow-bg-text"><?php echo $product->name ?></span> <?=$catName?><br />
32<p class="price_txt">starts from <span class="price_hd"><?php echoMage::helper('core')->currency($product->price) ?> </span> </p>
33</div>
34 
35<br class="spacer" /></div>
36<!--
37<h4><?php echo $product->name ?></h4>
38</a>
39<small><?php echo $this->__('Total soled quantities') ?>: <?php echo(int)$product->ordered_qty ?></small><br />
40 
41<a href="<?php echo $productUrl ?>" title="View <?php echo $product->name ?>">
42<img src="<?php echo $this->helper('catalog/image')->init($product, 'image')->resize(120); ?>" alt="Product image"  />
43</a> <br />
44 
45<?php echo $this->__('Categories: ') ?><?php echo $categories ?>
46<p><?php echo $product->short_description ?></p>
47-->
48<?php endif$counter++; ?>
49<?php endforeach; ?>
In this way you can get the list of best selling product.
How to use this code:
  • Just create one phtml file as[highsold.phtml] in category/product directory
  • Paste this code in that file [highsold.phtml]
  • Now go to layout/cms.xml file and add following line of code
1<cms_page>
2 
3<reference name="content">
4 
5<strong> <block type="catalog/product" name="highsold" as="highsold"template="catalog/product/highsold.phtml" /></strong>
6<block type="cms/page" name="cms_page" />
7</reference>
8</cms_page>
By this way you are creating a block named as highsold
  • Now if you want to display the product list on home page.Just go to home.phtml file and ad following lines as<?php echo $this->getChildHtml(‘highsold’) ?>
  • next go to admin/cms/manage pages/ and select home pagel. In content text area add following lines as
1{{block type="core/template" name="default_home_page" template="cms/default/home.phtml" }}
Now you should see the products perfectly displayed on home page!

Method3

  1. Create a category to contain the featured products. Call it e.g. Featured” or “Home Page”.
  2. Set “Is Active” to No. That way, it won’t display in the catalog menu.
  3. After saving the category, please note what ID it gets. You can see it in the last part of the URL. If the URL ends withcatalog_category/edit/id/8/, the ID is 8. On later version, the ID is next to the category name.
  4. Add products for the home page to the new category.
  5. Edit the Home Page (CMS → Manage Pages → Home Page) and add the following content, where 8 should be replaced by your category ID:
{{block type="catalog/product_list" category_id="8" template="catalog/product/list.phtml"}}
If you want a view that is different from the category lists, you can copy and modify list.phtml and change the path above.

Method 4

To show bestseller at Top seller at home page.so that best products should come on home page.For that follow the following steps
Create a Bestseller.php file and put it here :
app/code/local/Mage/Catalog/Block/Product/Bestseller.php
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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('id')
            ->addAttributeToSelect(array('name', 'price', 'small_image'))
            ->setStoreId($storeId)
            ->addStoreFilter($storeId)
            ->setOrder('ordered_qty', 'desc'); // most best sellers on top
        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
        $products->setPageSize(3)->setCurPage(1);
        $this->setProductCollection($products);
    }
}

Create bestseller.phtml file and put it here :
app/design/frontend/yourtheme/template/catalog/product/bestseller.phtml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php if (($_products = $this->getProductCollection()) && $_products->getSize()): ?>
<div class="home-page-cntr">
<?php $i=0; foreach ($_products->getItems() as $_product): ?>
    <?php if ($i>5): continue; endif; ?>
<div class="home-page-item">
        <div class="home-page-img">
            <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>">
                 <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(65,65); ?>" alt="<?php echo $this->htmlEscape($_product->getName()) ?>"/>
            </a>
            <?php echo $_product->getDescription(); //also getShortDescription ?>
        </div>
        <div class="home-page-txt">
            <p><a class="product-name" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>)"><?php echo $_product->getName() ?></a></p>
            <?php //echo $this->helper('review/product')->getSummaryHtml($_product, 'short') //product review link ?>
            <?php echo $this->getReviewsSummaryHtml($_product, false, true)?>
            <?php //echo $this->helper('catalog/product')->getPriceHtml($_product) ?>
            <?php echo $this->getPriceHtml($_product) ?>
            <?php echo $_product->getProductId(); ?>
            <?php if($_product->getevent_date()) {echo $_product->getevent_date();} ?>
</div>
</div>
<?php $i++; endforeach; ?>
<?php for($i;$i%5!=0;$i++): ?>
    <?php endfor ?>
</div>
<?php endif; ?>
now put this line where you want to view best selling products..
you can use through block or through XML also
{{block type="catalog/product_bestseller" template="catalog/product/bestseller.phtml"}}

<block type="catalog/product_bestseller" name="bestseller" template="catalog/product/bestseller.phtml">


By PHP with 5 comments

5 comments:

Hello
Thank you for the post and describing so many methods how to create magento bestsellers blocks.
But i use magento extension such as http://amasty.com/improved-sorting.html. It gives me more opportunity to create and customize different blocks such as most viewed, new products and etc. And of course lots of different sorting options are very helpful too.

Thank you, i hope this information will be helpful for someone.

Have a nice day,
Jim

While most people spend time spreading useless information on Facebook, here

Method 1 does not work at Magento 1.9.1.0

Post a Comment

    • Popular
    • Categories
    • Archives