Showing posts with label How to dynamically display Magento store's bestselling products. Show all posts
Showing posts with label How to dynamically display Magento store's bestselling products. Show all posts

Friday 12 September 2014

Bestseller products in magento


Bestseller products in magento

<?php
$totalPerPage = ($this->show_total) ? $this->show_total : 5;
$cnt = 1;
$visibility = array(
 Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
 Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
           );

$storeId = Mage::app()->getStore()->getId();
$_productCollection = Mage::getResourceModel('reports/product_collection')
                              ->addAttributeToSelect('*')
                              ->addOrderedQty()
                              ->addAttributeToFilter('visibility', $visibility)
                              ->setOrder('ordered_qty', 'desc');
?>
<?php foreach($_productCollection as $product): ?>
<?php if($cnt <= $totalPerPage): ?>
        <?php $productUrl =  $product->getProductUrl() ?>
        <a href="<?php echo $productUrl ?>" title="View <?php echo $product->name ?>">
          <h2><?php echo $product->name ?></h2>
        </a>
        <small><?php echo $this->__('Total soled qty') ?>: <?php echo (int)$product->ordered_qty ?></small><br />
     
        <a href="<?php echo $productUrl ?>" title="<?php echo $product->name ?>">
        <img src="<?php echo $this->helper('catalog/image')->init($product, 'image')->resize(220,300); ?>" alt=""  />
        </a> <br />
        <p><?php echo $product->short_description ?></p>
    <?php endif; $cnt++; ?>
<?php endforeach; ?>