Magento Best Selling or Top Selling Product

Magento Best Selling or Top Selling Product

Here, we have written article about How to display Best Selling or Top Selling Product in Magento?. To show Best selling or Top selling Product 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

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

<?php

$_products = $this->getProductCollection();
Mage::getSingleton(‘cataloginventory/stock’)->addInStockFilterToCollection($_products); // for qty stock check

for print Query

//echo $_products->getSelect();
//print $_products->getSize();

if ($_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

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

Advertisements

7 thoughts on “Magento Best Selling or Top Selling Product

    1. Hi Sebastian Schalipp,
      Thanks for your comments!.
      Above coding is for 1.8.x and above version. Please check in 1.8.x version.
      If you still giving error in 1.8.x or you work on 1.5.1 than update code.
      Please let me know any thing else.

      cheers.
      Ashwin

    1. Hello Ovidiu Cristian Balan,

      Please check code. It may be help you.

      //Get Bestselling products for last 30 days

      class Mage_Catalog_Block_Product_Bestseller extends Mage_Catalog_Block_Product_Abstract{
      public function __construct()
      {
      parent::__construct();

      // number of products to display
      $productCount = 5;

      //store id
      $storeId = Mage::app()->getStore()->getId();

      // get today and last 30 days time
      $today = time();
      $last = $today – (60*60*24*30);

      $from = date(“Y-m-d”, $last);
      $to = date(“Y-m-d”, $today);

      $products = Mage::getResourceModel(‘reports/product_collection’)
      ->addOrderedQty()
      ->addAttributeToSelect(‘id’)
      ->addAttributeToSelect(array(‘name’, ‘price’, ‘small_image’)) // (*) for all
      ->addOrderedQty($from, $to) // for last 30 days
      ->setStoreId($storeId)
      ->addStoreFilter($storeId)
      ->setOrder(‘ordered_qty’, ‘desc’) // most best sellers on top
      ->setPageSize($productCount); // for display number of product

      Mage::getSingleton(‘catalog/product_status’)->addVisibleFilterToCollection($products);
      Mage::getSingleton(‘catalog/product_visibility’)->addVisibleInCatalogFilterToCollection($products);
      $products->setPageSize(3)->setCurPage(1);
      $this->setProductCollection($products);
      }
      }

      Thanks,
      Ashwin

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s