Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to display best selling products

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 133
    Comment on it

    In Magento store some times we are required to display best selling products on store’s home page or some other places. To do so we will perform the same as :

    first of all we have to create a file in the path  app/code/local/Mage/Catalog/Block/Product/Bestseller.php with the following code in it :

    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);
        }
    }

    Secondly,  Create another file in this path app/design/frontend/default/YourTheme/template/catalog/product/bestseller.phtml with the following code in it. The below code will fetch the best selling products on the page but we have to call this code in a block. 

    <?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; ?>

    Now finally we have to call only the above code to display the best selling products on our store page this can be done as :

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

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: