PHP Magento Tips

How to Display Best Selling Products in Magento

Rate this posting:
{[['']]}

Follow Below Steps to display bestselling products on a page in Magento.

Step 1: Create a new file for Block

Create new file in app/code/local/Mage/Catalog/Block/Product/Bestseller.php and add below code in it.

<?php

/**

 * Catalog Product Bestseller Block

 *

 * @author Vijay Rami

 */

class Mage_Catalog_Block_Product_Bestseller extends Mage_Catalog_Block_Product_Abstract

{

    public function getCollection()

    {

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

        $collection = Mage::getResourceModel('reports/product_collection')

            ->addOrderedQty()

            ->setStoreId($storeId)

            ->addStoreFilter($storeId)

            ->setOrder('ordered_qty', 'desc');

        if (Mage::helper('catalog/product_flat')->isEnabled()) {

            $collection->getSelect()

                ->joinInner(array('e2' => 'catalog_product_flat_' . $storeId), 'e2.entity_id = e.entity_id');

        } else {

            $collection->addAttributeToSelect('*')

                ->addAttributeToSelect(array('name', 'price', 'small_image'));

        }

        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);

        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);

        if ($categoryId = $this->getCategory()) {

            $category = Mage::getModel('catalog/category')->load($categoryId);

            $collection->addCategoryFilter($category);

        }

        $collection->setPage(1, $this->getLimit());

        // add below line if you want to display Rating and Review Summary

        Mage::getModel('review/review')->appendSummary($collection);

        return $collection;

    }

}


Step 2: Create a template file for the new block

add template file app/design/frontend/rwd/default/template/catalog/product/bestseller.phtml and add below code in it.

<?php
/**
 * Bestseller Products block template
 *
 * @see Mage_Catalog_Block_Product_Bestseller
 */
?>
<div class="block block-list block-viewed">
    <?php $products = $this->getCollection(); ?>
    <?php if (0 < $products->getSize()) { ?>
    <div class="block-title">
        <strong><span><?php echo $this->__($this->getHeader()) ?></span></strong>
    </div>
    <div class="block-content"> 
            <table class="bestseller-table">
                <tr>
                    <?php foreach ($products as $p) { ?>
                        <td style="padding: 15px 15px 0px 15px;">
                            <a href="<?php echo $p->getProductUrl() ?>" title="<?php echo $this->htmlEscape($p->getName()) ?>" class="product-image">
                                <img src="<?php echo $this->helper('catalog/image')->init($p, 'small_image')->resize(125) ?>" width="125" height="125" alt="<?php echo $this->htmlEscape($p->getName()) ?>" />
                            </a>
                            <h3 class="product-name">
                                <a href="<?php echo $p->getProductUrl() ?>" title="<?php echo $this->htmlEscape($p->getName())?>">
                                    <?php echo $this->htmlEscape($p->getName()) ?>
                                </a>
                            </h3>
                            <div class="a-center">
                            <?php if($p->getRatingSummary()): ?>
                       <?php echo $this->getReviewsSummaryHtml($p, 'short'); ?>
                   <?php endif; ?>
                            <?php echo $this->getPriceHtml($p, true) ?>
                            </div>
                        </td>
                    <?php } ?>
                </tr>
                <tr>
                    <?php foreach ($products as $p) { ?>
                    <td style="padding: 0px 15px 15px;">
                    <div class="a-center">
                        <?php if($p->isSaleable()): ?>
                            <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($p) ?>')">
                                <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; ?>
                    </div>
                    <ul class="add-to-links">
                    <?php if ($this->helper('wishlist')->isAllow()) : ?>
                        <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($p) ?>"><?php echo $this->__('Add to Wishlist') ?></a></li>
                    <?php endif; ?>
                    <?php if($_compareUrl=$this->getAddToCompareUrl($p)): ?>
                        <li class="last"><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>"><?php echo $this->__('Add to Compare') ?></a></li>
                    <?php endif; ?>
                 </ul>
                    </td>
                    <?php } ?>
                </tr>
            </table>      
    </div>
    <?php } ?>
</div>

Step 3: Now you need to call the created block, to do that go to Backend  CMS > Pages and Edit Page ‘Home page’ and add the following lines of code at last:

{{block type="catalog/product_bestseller" template="catalog/product/bestseller.phtml" header="Bestsellers" limit=4}}

Also you can add a new block via layout update:

<reference name="content">
        <block type="catalog/product_bestseller" name="bestseller" template="catalog/product/bestseller.phtml" before="-">
            <action method="setLimit"><limit>3</limit></action>
            <action method="setHeader"><header>Best Sellers</header></action>
        </block>
</reference>

For instance, try to add this block to the Category View page here:

to do that go to Backend Catalog > Manage Categories and Click needed category in the category tree and click on 'Custom Design' tab.

Add above code in 'Custom Layout Update' Field to display Best Selling Product in that category.

Feel free to manipulate the result using the header and the limit variables.

SUPEE-6788 update

When adding the block to the CMS page or Static block and using the following code:

{{block type="catalog/product_bestseller" template="catalog/product/bestseller.phtml" header="Bestsellers" limit=4}}

you need to create a permission for this block:

from Backend go to System – > Permissions – > Blocks – > '+Add New Block' button



This manipulation is needed to be done for Magento 1.9.2.2 and higher or after installing SUPEE-6788 security patch.

Adding the block via Custom Layout Update works well in any situation:

<reference name="content">

<block type="catalog/product_bestseller" name="bestseller" template="catalog/product/bestseller.phtml" before="-">

<action method="setLimit"><limit>4</limit></action>

<action method="setHeader"><header>Best Sellers</header></action>

</block>

</reference>

26.01.2016 update

The code now works with flat catalog, plus the category variable is added, and you can use a certain category for the product selection.

Add Magento bestsellers via CMS block:

{{block type="catalog/product_bestseller" template="catalog/product/bestseller.phtml" header="Bestsellers" limit=4 category=24}}


Add Magento bestsellers via layout update:

<reference name="content">

    <block type="catalog/product_bestseller" name="bestseller" template="catalog/product/bestseller.phtml" before="-">

        <action method="setLimit"><limit>4</limit></action>

        <action method="setHeader"><header>Best Sellers</header></action>

        <action method="setCategory"><category>24</category></action>

    </block>

</reference>
Tag : Magento
0 Comments On "How to Display Best Selling Products in Magento "

Back To Top