Wednesday, June 10, 2009

Bestselling Products in Magento

Magento Tutorials: Bestselling Products in Magento

Create new block class that will instantiate the Bestselling products

Create a new file, and directories: app/code/local/Mycompany/Bestsellers/Block/Product/Bestsellers.php

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

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

$products = Mage::getResourceModel('reports/product_collection')
->addOrderedQty()
->addAttributeToSelect('*')
->setStoreId($storeId)
->addStoreFilter($storeId)
->setOrder('ordered_qty', 'desc');

Mage
::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
Mage
::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);

$products->setPageSize(5)->setCurPage(1);

$this->setProductCollection($products);
}
}

Create a new config.xml for Bestselling products

Create a new file, and directories: app/code/local/Mycompany/Bestsellers/etc/config.xml

<?xml version="1.0"?>
<config>
<modules>
<Mycompany_Bestsellers>
<version>0.1.0</version>
</Mycompany_Bestsellers>
</modules>
<global>
<resources>
<bestsellers_setup>
<setup>
<module>Mycompany_Bestsellers</module>
</setup>
<connection>
<use>core_setup</use>
</connection>
</bestsellers_setup>
<bestsellers_write>
<connection>
<use>core_write</use>
</connection>
</bestsellers_write>
<bestsellers_read>
<connection>
<use>core_read</use>
</connection>
</bestsellers_read>
</resources>
<blocks>
<catalog>
<rewrite>
<product_bestsellers>
Mycompany_Bestsellers_Block_Product_Bestsellers
</product_bestsellers>
</rewrite>
</catalog>
</blocks>
</global>
</config>

Add new Bestsellers.phtml to your template

app/design/frontend/default/default/template/catalog/product/bestsellers.phtml

<?php $_productCollection=$this->getProductCollection() ?>
<div>Best Selling Products</div>
<?php if(!$_productCollection->count()): ?>
<div class="note-msg">
<?php echo $this->__('There are no products matching the selection.') ?>
</div>
<?php else: ?>
<?php // Grid Mode ?>
<div class="listing-type-grid catalog-listing">
<?php $_collectionSize = $_productCollection->count() ?>
<?php $i=0; foreach ($_productCollection as $_product): ?>
<?php if($i++%2==0): ?>
<ol class="grid-row">
<?php endif; ?>
<li class="item">
<p class="product-image">
<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(135, 135); ?>" width="135" height="135" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" />
</a>
</p>
<h5><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a></h5>
<p class="add-to">
<?php if ($this->helper('wishlist')->isAllow()) : ?>
<a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-cart"><?php echo $this->__('Add to Wishlist') ?></a>
<?php endif; ?>
<?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?><br/>
<a href="<?php echo $_compareUrl ?>"><?php echo $this->__('Add to Compare') ?></a>
<?php endif; ?>
</p>
<?php echo $this->getPriceHtml($_product, true) ?>
<div class="clear"></div>
<?php if($_product->getRatingSummary()): ?>
<?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
<?php endif; ?>
<?php if($_product->isSaleable()): ?>
<div class="button_pad" ><button class="form-button" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><?php echo $this->__('Add to Cart') ?></span></button></div>
<?php else: ?>
<div class="out-of-stock"><?php echo $this->__('Out of stock') ?></div>
<?php endif; ?>
</li>
<?php if($i%2==0 || $i==$_collectionSize): ?>
</ol>
<?php endif; ?>
<?php endforeach ?>
<script type="text/javascript">decorateGeneric($$('.grid-row'), 'last', 'odd', 'even']);</script>
</div>
<?php endif; ?>

Activate your module

create new Mycompany_Bestsellers.xml

<?xml version="1.0"?>
<config>
<modules>
<Mycompany_Bestsellers>
<active>true</active>
<codePool>local</codePool>
</Mycompany_Bestsellers>
</modules>
</config>

Add your block to the correct page:

{{block type="catalog/product_bestsellers" name="home.catalog.product.bestsellers" alias="product_bestsellers" template="catalog/product/bestsellers.phtml"}}

then Refresh your cache management.

check it and It's works well.


Tags: Catalog, Bestselling Products, Bestseller Products

3 comments:

  1. Where should I place the "activate" code? In app/design/default/default/layout?

    ReplyDelete
  2. "Hello
    Thank you for the post and describing so many methods how to create magento bestsellers blocks.
    But i use magento extension such as http://amasty.com/improved-sorting.html. It gives me more opportunity to create and customize different blocks such as most viewed, new products and etc. And of course lots of different sorting options are very helpful too.

    Thank you, i hope this information will be helpful for someone.

    Have a nice day,
    Jim"

    ReplyDelete