Display Best Selling Products on your Magento Store

Magento

It's a good idea to tell your audience which products are "best sellers" on your e-store. Most often, people do want to know what others have viewed on your store. When you display these products, you are actually making your audience stay on your store for a longer while. Buying time will help you convert better, and that is what you need to look at. How can you display "best selling" or "

most viewed products on your e-store? Here is how you can add these features programmatically.

Display Most Viewed Products

First you will need to create a module named MostViewed.php in the following path

app/code/local/Mage/Catalog/Block/Product/Mostviewed.php

Copy-paste the following code to this file

class Mage_Catalog_Block_Product_Mostviewed extends Mage_Catalog_Block_Product_Abstract{
public function __construct(){
    parent::__construct();
    $storeId    = Mage::app()->getStore()->getId();
    $products = Mage::getResourceModel('reports/product_collection')
        ->addOrderedQty()
        ->addAttributeToSelect('*')
        ->addAttributeToSelect(array('name', 'price', 'small_image'))
        ->setStoreId($storeId)
        ->addStoreFilter($storeId)
        ->addViewsCount();
    Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
    Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
     $products->setPageSize(5)->setCurPage(1);
    $this->setProductCollection($products);
}}

Go to app/design/frontend/default/YourTheme/template/catalog/product/mostviewed and paste the following code in this path

<?php if (($_products = $this->getProductCollection()) && $_products->getSize()): ?>
<div class=" most_viewed">
<div class="mv_title"><?php echo $this->__('These Products Are Popular Right Now!') ?></div>
<?php $_collectionSize = 5;//count($_products->getItems()); echo $_collectionSize; ?>
<ul class="products-grid" id="products-grid-table">
<?php $i=1; foreach ($_products->getItems() as $_product): ?>
    <li id="td_<?php echo $i;?>" <?php if($i%5==0 or $i==$_collectionSize){echo 'class="last"';} ?> >
        <div id="cont_<?php echo $i;?>">        
            <a class="product-image" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>">
                <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135); ?>" width="135" height="135" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />
            </a>
            <h3 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a></h3>
            <div class="a-center">                        
                <?php if($_product->getRatingSummary()): ?>
                    <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
                <?php endif; ?>
                <?php echo $this->getPriceHtml($_product, true) ?>
                <?php if($_product->isSaleable()): ?>
                    <button class="button" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><span><?php echo $this->__('Add to Cart') ?></span></span></span></button>
                    <div class="clear"></div>
                <?php else: ?>
                    <p class="availability"><span class="out-of-stock"><?php echo $this->__('Out of stock') ?></span></p>
                    <div class="clear"></div>
                <?php endif; ?>
                <ul class="add-to-links">
                    <?php if ($this->helper('wishlist')->isAllow()) : ?>
                    <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>"><?php echo $this->__('Add to Wishlist') ?></a></li>
                    <?php endif; ?>
                    <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
                    <li class="last"><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>"><?php echo $this->__('Add to Compare') ?></a></li>
                    <?php endif; ?>
                </ul>
            </div>
        </div>
    </li>
<?php $i++; endforeach; $kol = $_collectionSize; ?>
</ul>
</div>
<?php endif; ?>

With this code, the most viewed products would be displayed along the front end. Now, let's add a block that will display the most viewed products.

{{block type="catalog/product_mostviewed" template="catalog/product/mostviewed.phtml"}}

Display Best Selling Products

Let's begin by creating a module

app/code/local/Mage/Catalog/Block/Product/Bestseller.php

Copy-paste the following code to this path

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('*')
            ->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 the phtml file for best seller along the following path

app/design/frontend/default/YourTheme/template/catalog/product/bestseller.phtml. Paste the code given below to it.

<?php if (($_products = $this->getProductCollection()) && $_products->getSize()): ?>
<div class="page-title">
    <h2><?php echo $this->__('Best Seller Products') ?></h2>
</div>
<?php $_collectionSize = count($_products->getItems()) ?>
<table class="products-grid" id="products-grid-table">
<?php $i=1; foreach ($_products->getItems() as $_product): ?>
    <?php if ($i%1!==0): ?>
    <tr>
    <?php endif ?>
        <td id="td_<?php echo $i;?>" <?php if($i%3==0 or $i==$_collectionSize){echo 'class="last"';} ?> >
        <?php contentBlock('top') ?>
        <div id="cont_<?php echo $i;?>">
            <h3 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a></h3>
            <a class="product-image" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>">
                <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(122, 109); ?>" width="122" height="109" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />
            </a>
            <div class="a-center">                        
                <?php if($_product->getRatingSummary()): ?>
                    <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
                <?php endif; ?>
                <?php echo $this->getPriceHtml($_product, true) ?>
                <?php if($_product->isSaleable()): ?>
                    <button class="button" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><span><?php echo $this->__('Add to Cart') ?></span></span></span></button>
                    <div class="clear"></div>
                <?php else: ?>
                    <p class="availability"><span class="out-of-stock"><?php echo $this->__('Out of stock') ?></span></p>
                    <div class="clear"></div>
                <?php endif; ?>
                <ul class="add-to-links">
                    <?php if ($this->helper('wishlist')->isAllow()) : ?>
                        <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>"><?php echo $this->__('Add to Wishlist') ?></a></li>
                    <?php endif; ?>
                    <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
                        <li class="last"><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>"><?php echo $this->__('Add to Compare') ?></a></li>
                    <?php endif; ?>
                </ul>
                <?php if($_product->getevent_date()) {echo $_product->getevent_date();} ?>
            </div>
        </div>
    </td>
    <?php if ($i%3==0 or $i==$_collectionSize): ?>
   </tr>
    <?php endif ?>
  <?php $i++; endforeach; $kol = $_collectionSize; ?>
</table>
<?php endif; ?>

With this code, the best selling products have been created. Now, you need to add a block to display the best selling products. Use the code given below to perform this task

{{block type="catalog/product_bestseller" template="catalog/product/bestseller.phtml"}}

Deepa, a technical writer with Semaphore Software, who now devotes her time in advising its clients to hire offshore magento developers. She offers information as well as tips and latest trends in this domain. Her love for reading helps her constantly provide latest information on different technical and design aspects of Magento

Deepa Ranganathan
  • magento
  • customization
  • store
  • products
By Deepa Ranganathan On 29 Apr, 15  Viewed: 238

Other blogs you may like

How to Manage the Backend Functionality of Your Magento Store?

Helper contains the necessary functions for you; you can call in a helper from anywhere. An example of helper is as below $helper = Mage::helper('monhelper'); The call can be related to $helper = Mage::helper('monhelper/data'); **Let's create a helper file** You will need to... By Deepa Ranganathan   On 17 Jul 2015  Viewed: 142

How to Programmatically Create a Dropdown Login for your Magento Store?

If you want to improve the interface of your Magento store, then you must include the dropdown login form. This would help create an interactive base for your consumers, and offer convenience too! Let’s see how you can include the form programmatically. Go to... By Deepa Ranganathan   On 07 Jul 2015  Viewed: 1,151

3 Ways to Track Shoppers Checking-in From Different Devices

Smart devices are just increasing and improving with time. The digital era has definitely introduced many devices that will offer convenience and ease out your problems. With multiple platforms available for logging into your favourite store, it becomes difficult for you as a store owner or a... By Deepa Ranganathan   On 18 Jun 2015  Viewed: 185

5 Tips to Use local.xml to Customize Your E-Store

When you plan out a new theme for your Magento store, you begin designing it by using the default theme as your base. Very rarely would you create a new theme from the scratch. The reason being the default theme brings with it most of the elements like standard features, that are required by the... By Deepa Ranganathan   On 04 May 2015  Viewed: 142

Code Hacks to Make your Website Theme Appealing

Wordpress has some amazing and highly powerful themes that you can use to improve the functionality and aesthetic appeal of your website. While, there are functionalities available with these themes, you will see that there is a huge possibility of customizing the theme with some interesting code... By Deepa Ranganathan   On 03 Jun 2015  Viewed: 153