Step 1:
Create a file app/code/local/Mage/Catalog/Block/Product/Bestseller.php and the following lines of 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); } }Step 2:
Create a file app/design/frontend/default/YourTheme/template/catalog/product/bestseller.phtml file and add the following lines of code in it
<?php if (($_products = $this->getProductCollection()) && $_products->getSize()): ?><?php $_collectionSize = count($_products->getItems()) ?><?php echo $this->__('Best Seller Products') ?>
<?php contentBlock('top') ?>
<?php echo $this->htmlEscape($_product->getName()) ?>
<?php if($_product->getRatingSummary()): ?>
<?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
<?php endif; ?>
<?php echo $this->getPriceHtml($_product, true) ?>
<?php if($_product->isSaleable()): ?>
<?php else: ?>
<?php echo $this->__('Out of stock') ?> <?php endif; ?>
|
<?php if ($i%3==0 or $i==$_collectionSize): ?>
This above files will create a list of best selling products which can be shown anywhere on your Magento store. All you have to do is place the following line of code block in your template to show the best selling products.
{{block type="catalog/product_bestseller" template="catalog/product/bestseller.phtml"}}
No comments:
Post a Comment