many developers want use the feature of showing Best Selling products Most Viewed products or New products in home page right column or left column , this tutorials of how create the query for this function ,display absolutely or by categories :
Show Best Selling products :
Step 1: Create this file app/code/local/YourCompany/YourModule/Block/Product/Bestseller.php in your module and add content :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
<?php
class YourCompany_YourModule_Block_Bestseller extends Mage_Catalog_Block_Product_Abstract
{
public function _prepareLayout()
{
return parent::_prepareLayout();
}
public function getProducts()
{
$storeId = Mage::app()->getStore()->getId();
$cateids = "22,24,34";
$arr_catid = explode(",", $cateids);
if($cateids != null)
{
$arr_productids = $this->getProductByCategory();
$products = Mage::getResourceModel('reports/product_collection')
//->addAttributeToSelect('*')
->addOrderedQty()
->addMinimalPrice()
->addAttributeToSelect(array('name', 'price', 'small_image'))
->setStoreId($storeId)
->addStoreFilter($storeId)
->setOrder('ordered_qty', 'desc');
$products->joinField('category_id',
'catalog/category_product',
'category_id',
'product_id=entity_id',
null,
'left');
$products->addAttributeToFilter('category_id', array('in' => $arr_catid));
}
else {
$products = Mage::getResourceModel('reports/product_collection')
->addOrderedQty()
//->addAttributeToSelect('*')
->addMinimalPrice()
->addAttributeToSelect(array('name', 'price', 'small_image'))
->setStoreId($storeId)
->addStoreFilter($storeId)
->setOrder('ordered_qty', 'desc');
}
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
$products->setPageSize($this->getConfig('qty'))->setCurPage(1);
//$pro_Ids = array();
//foreach($products as $product){
//$pro_Ids[] = $product->getId();
//}
//$colection = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('entity_id', array('in' => $pro_Ids));
$this->setProductCollection($products);
}
}
|
Step 2: Create a file app/design/frontend/default/YourTheme/template/YourModule/catalog/product/bestseller.phtml file and add the following lines of code in it
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
<?php $this->getProducts() ?>
<?php $_productCollection=$this->getProductCollection()
$w_thumbnail="100px";
$h_thumbnail="100px";
?>
<div class="ma-bestseller-vertscroller-wrap">
<div class="ma-bestseller-vertscroller-title"><h2><?php echo "Best Seller Products"?></h2></div>
<?php if(!$_productCollection->count()): ?> <!-- start if exist product -->
<div class="ma-bestseller-vertscoller-content">
<p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p>
</div>
<?php else: ?>
<div class="ma-bestseller-vertscoller-content">
<ul id ="ma-bestseller-vertscroller-left" class="jcarousel jcarousel-skin-tango bestseller-vertscroller">
<?php $_collectionSize = $_productCollection->count() ?>
<?php if( !$this->getConfig('items') ) { $_columnCount = 1 ; } else { $_columnCount = $this->getConfig('items'); }?>
<?php $i=1; foreach ($_productCollection as $_product): ?>
<li class="item <?php if($i==1): ?> first<?php elseif($i == $_collectionSize): ?> last<?php endif; ?>" >
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize($w_thumbnail, $h_thumbnail); ?>" width="<?php echo $w_thumbnail?>" height="<?php echo $h_thumbnail?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" /></a>
<div class="box-bestseller">
<h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a></h2>
<?php $i++; ?>
<?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
<?php echo $this->getPriceHtml($_product, true) ?>
<div class="actions">
<?php if($_product->isSaleable()): ?>
<button type="button" 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->__('+ 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->__('+ Compare') ?></a></li>
<?php endif; ?>
</ul>
</div>
</div>
</li>
<?php endforeach ?>
</ul>
<script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])</script>
</div>
<?php endif; ?>
</div>
|
Show Most Viewed products :
Step 1: Create this file app/code/local/YourCompany/YourModule/Block/Product/Mostviewed.php in your module and add content :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
<?php
class Magentothem_Mostviewedproduct_Block_Mostviewed extends Mage_Catalog_Block_Product_Abstract
{
public function _prepareLayout()
{
return parent::_prepareLayout();
}
public function _getProductCollection()
{
$storeId = Mage::app()->getStore()->getId();
$cateids = "22,24,34";;
$arr_catid = explode(",", $cateids);
if($cateids != null) {
// $arr_productids = $this->getProductByCategory();
$products = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('*')
->addMinimalPrice()
->addUrlRewrite()
->addTaxPercents()
->addAttributeToSelect(array('name', 'price', 'small_image')) //edit to suit tastes
->setStoreId($storeId)
->addStoreFilter($storeId)
->addViewsCount();
$products->joinField('category_id',
'catalog/category_product',
'category_id',
'product_id=entity_id',
null,
'left');
$products->addAttributeToFilter('category_id', array('in' => $arr_catid));
}
else {
$products = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('*')
->addMinimalPrice()
->addUrlRewrite()
->addTaxPercents()
->addAttributeToSelect(array('name', 'price', 'small_image')) //edit to suit tastes
->setStoreId($storeId)
->addStoreFilter($storeId)
->addViewsCount();
}
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
$products->setPageSize($this->getConfig('qty'))->setCurPage(1);
$this->setProductCollection($products);
}
}
|
Step 2: Create a file app/design/frontend/default/YourTheme/template/YourModule/catalog/product/mostviewed.phtml file and add the same line as best selling products.
Show New products :
Step 1: Create this file app/code/local/YourCompany/YourModule/Block/Product/New.php in your module and add content :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
<?php
class YourCompany_YourModule_Block_New extends Mage_Catalog_Block_Product_Abstract
{
public function _prepareLayout()
{
return parent::_prepareLayout();
}
public function getProducts()
{
$todayDate = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$storeId = Mage::app()->getStore()->getId();
$cateids = $this->getConfig('catsid');
if($cateids){
$arr_catid = explode(",", $cateids);
$products = Mage::getResourceModel('catalog/product_collection')
->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addMinimalPrice()
->addUrlRewrite()
->addTaxPercents()
->addStoreFilter()
->addAttributeToFilter('category_id', array('in' => $arr_catid))
->addAttributeToFilter('news_from_date', array('date'=>true, 'to'=> $todayDate))
->addAttributeToFilter(array(array('attribute'=>'news_to_date', 'date'=>true, 'from'=>$todayDate), array('attribute'=>'news_to_date', 'is' => new Zend_Db_Expr('null'))),'','left')
->addAttributeToSort('news_from_date','desc');
} else {
$root_cat = Mage::app()->getStore()->getRootCategoryId();
$products = Mage::getResourceModel('catalog/product_collection')
->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addMinimalPrice()
->addUrlRewrite()
->addTaxPercents()
->addStoreFilter()
->addAttributeToFilter('category_id', array('in' => $root_cat))
->addAttributeToFilter('news_from_date', array('date'=>true, 'to'=> $todayDate))
->addAttributeToFilter(array(array('attribute'=>'news_to_date', 'date'=>true, 'from'=>$todayDate), array('attribute'=>'news_to_date', 'is' => new Zend_Db_Expr('null'))),'','left')
->addAttributeToSort('news_from_date','desc');
}
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
$products->setPageSize($this->getConfig('qty'))->setCurPage(1);
$this->setProductCollection($products);
}
}
|
Last step you can use this line in home page for showing anything you want in cms page :
{{block type=”YourModule/product_bestseller” template=”catalog/product/bestseller.phtml”}}
Thanks for the article!
Do you happen to create a ready extension for sorting? I’m not good at coding so all these steps seem hard for me