Display Bestsellers for Specific Magento Categories

Display Bestsellers for Specific Magento Categories

If you have several categories in your Magento store, you may be interested in showing the top selling products for each of your categories. While Magento does provide you with a report of the bestselling products, you need to extend the core functionality in order to show these products to your customers. If you need to show it by category, you need to take it a step further!

First, you need to create a new class. Create this file at app/code/local/Mage/Catalog/Block/Product/Bestseller.php:

<?php class Mage_Catalog_Block_Product_Bestseller extends Mage_Catalog_Block_Product_Abstract {

    protected function _beforeToHtml()
    {
       $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);
		
	if($categoryId=$this->getData('category_id')){
            $category = Mage::getModel('catalog/category')->load($categoryId);
            $products->addCategoryFilter($category);
     	}
		
	$products->setPageSize(5)->setCurPage(1); // change the Page Size to how many products you want to show per category
		
        $this->setProductCollection($products);
		
        return parent::_beforeToHtml();
    }
}

Then, you have to create your template for how you want to show your products. My version has been heavily customized, but I’m providing a simple version here with the code for Grid view from the base/default package. Place this file in your theme as template/catalog/product/bestseller.phtml.

<?php if (($_products = $this->getProductCollection())): ?>
<ul class="top-products">
  <?php foreach ($_products->getItems() as $_product): ?>   
    <li>
      <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135); ?>" width="135" height="135" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a>
                <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></a></h2>
                <?php if($_product->getRatingSummary()): ?>
                <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
                <?php endif; ?>
                <?php echo $this->getPriceHtml($_product, true) ?>
                <div class="actions">
                    <?php if($_product->isSaleable()): ?>
                        <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
                    <?php else: ?>
                        <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                    <?php endif; ?>
                    <ul class="add-to-links">
                        <?php if ($this->helper('wishlist')->isAllow()) : ?>
                            <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a></li>
                        <?php endif; ?>
                        <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
                            <li><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>" class="link-compare"><?php echo $this->__('Add to Compare') ?></a></li>
                        <?php endif; ?>
                    </ul>
                </div>
    </li>
  <?php endforeach; ?>
</ul>
<?php endif; ?>

Place this on any of your CMS pages where you want to show this list of bestselling products, changing out the category_id for the category ID that you want to show the bestsellers for:

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

You can use this code to create a bestsellers list for each of your store’s categories, or a page dedicated to the most popular products in your store (organized by category). I’ve implemented it as a tabbed section on a store’s homepage for the 3 most important categories in the store.

Need help implementing this in your own Magento store? Feel free to contact or hire me!

Resources:

0 comments on “Display Bestsellers for Specific Magento CategoriesAdd yours →

Leave a Reply