Magento Best Selling or Top Selling Product

Topics focus on Web Developments, Programming, Database, Digital Marketing Tutorials, Code Snippets, SEO, Social Media Marketing and Inspirational articles.

Magento Best Selling or Top Selling Product

Magento Best Selling or Top Selling Product

Here, we have written article about How to display Magento Best Selling or Top Selling Product. To show Best selling or Top selling Product at Home page, you need to follow below process. You can display in sidebar or anywhere in product detail page by calling block. But here, we have put block Best products on home page. So you can get the idea.

For that follow the following steps:

First of all, 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
18
 
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
27
 
<?php if (($_products = $this->getProductCollection()) && $_products->getSize()): ?>
<div>
<?php $i=0; foreach ($_products->getItems() as $_product): ?>
    <?php if ($i>5): continue; endif; ?>
<div>
        <div>
            <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>
            <p><a 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

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

Add comment


 

Pin It on Pinterest

Share This