i have used below code to display most viewed products Mostviewed.php
<?php
/**
* Created by PhpStorm.
* User: coopso-26-1
* Date: 9/24/2015
* Time: 10:54 AM
*/
class Mage_Catalog_Block_Product_Mostviewed 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)
->addViewsCount();
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
$products->setPageSize(4)->setCurPage(1);
$this->setProductCollection($products);
}
}
and mostviewed.phtml
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE_AFL.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magento.com for more information.
*
* @category design
* @package base_default
* @copyright Copyright (c) 2006-2014 X.commerce, Inc. (http://www.magento.com)
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/
$_productCollection=$this->getLoadedProductCollection();
$_helper = $this->helper('catalog/output');
$this->setData('column_count',4);
$modules = Mage::getConfig()->getNode('modules')->children();
$modulesArray = (array)$modules;
if(isset($modulesArray['Tm_ProductListGallery'])) {
$productgallerylist_helper = Mage::helper('productlistgallery');
$productgallerylist_config = $productgallerylist_helper->getConfigData('home_grid');
}
?>
<?php
/*$products = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('*')
->addAttributeToSelect(array('name', 'price', 'small_image'))
->setStoreId($storeId)
->addStoreFilter($storeId)
->addViewsCount();
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
echo "<pre>";
print_r($_products);
*/?>
<?php if (($_products = $this->getProductCollection()) && $_products->getSize()): ?>
<div class="widget widget-popular-products container">
<div class="widget-title">
<h2><?php echo $this->__('Most Popular Products') ?></h2>
</div>
<div class="widget-products">
<?php echo $this->getPagerHtml() ?>
<?php $_columnCount = $this->getColumnCount(); ?>
<?php $i=0; foreach ($_products->getItems() as $_product): ?>
<?php if ($i++%$_columnCount==0): ?>
<ul class="products-grid row">
<?php endif ?>
<li class="item <?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?> col-xs-12 col-sm-3">
<div class="wrapper-hover">
<div class="product-image-container">
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>" class="product-image noSwipe">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize($productgallerylist_config['image_width'], $productgallerylist_config['image_height']) ?>" alt="<?php echo $this->stripTags($_product->getName(), null, true) ?>" /></a>
<?php
if ( !sizeof($_product->getData('list_swatch_attr_values')) ) :
if ($productgallerylist_config['active'] == '1'): ?>
<ul class="product-thumbs">
<?php
$product_id = $_product->getId();
$list_product = Mage::getModel('catalog/product')->load($product_id);
$count_thumbs = 1;
foreach ($list_product->getMediaGalleryImages() as $image) {
if($count_thumbs <= 3){?>
<li class="product-thumb">
<a href="<?php echo $this->helper('catalog/image')->init($_product, 'small_image', $image->getFile())->resize($productgallerylist_config['image_width']); ?>">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail', $image->getFile())->resize($productgallerylist_config['image_width'], $productgallerylist_config['image_height']); ?>" alt="" width="<?php echo $productgallerylist_config['thumb_size_w']; ?>" height="<?php echo $productgallerylist_config['thumb_size_h']; ?>"/>
</a>
</li>
<?php }
$count_thumbs++;
}
?>
</ul>
<?php endif;
endif; ?>
<div class="add-to-cart-grid">
<?php if ($_product->isSaleable()): ?>
<?php
if(in_array(121 , $_product->getCategoryIds())){?>
<button type="button" title="<?php echo $this->__('Enquire Now') ?>" class="button btn-cart btn-enquire"onclick="setLocation('<?php echo $_product->getProductUrl() .'#enquiry-form'?>')"><span><span><?php echo $this->__('Enquire Now') ?></span></span></button>
<?php }else{?>
<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 }
?>
<?php else: ?>
<p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>
</div>
</div>
<div class="product-info">
<h3 class="product-name">
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>">
<?php
$small_name = strip_tags($_product->getName());
if (strlen($small_name ) < 28) echo($small_name);
else { echo mb_substr($small_name, 0, 28,'UTF-8').'...';}
?>
</a>
</h3>
<?php
// Provides extra bloc->keepFrame(false)s on which to hang some features for products in the list
// Features providing UI elements targeting this block will display directly below the product name
if ($this->getChild('name.after')) {
$_nameAfterChildren = $this->getChild('name.after')->getSortedChildren();
foreach ($_nameAfterChildren as $_nameAfterChildName) {
$_nameAfterChild = $this->getChild('name.after')->getChild($_nameAfterChildName);
$_nameAfterChild->setProduct($_product);
echo $_nameAfterChild->toHtml();
}
}
?>
<?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
<?php echo $this->getPriceHtml($_product, true, '-widget-new-grid') ?>
<div class="add-to-cart-grid-2">
<?php if ($_product->isSaleable()): ?>
<?php
if(in_array(121 , $_product->getCategoryIds())){?>
<button type="button" title="<?php echo $this->__('Enquire Now') ?>" class="button btn-cart btn-enquire"onclick="setLocation('<?php echo $_product->getProductUrl() .'#enquiry-form'?>')"><span><span><?php echo $this->__('Enquire Now') ?></span></span></button>
<?php }else{?>
<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 }
?>
<?php else: ?>
<p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>
</div>
<div class="actions">
<a class="more-info" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>">More Info</a>
<ul class="add-to-links">
<?php if ($this->helper('wishlist')->isAllow()) : ?>
<li><a href="<?php echo $this->getAddToWishlistUrl($_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>
</div>
</div>
<?php $_product = Mage::getModel('catalog/product')->load($_product->getId()); ?>
<div class="label-product">
<?php if($_product->getData('new')){echo '<span class="new">'.$this->__('New').'</span>'; }?>
<?php if($_product->getData('sale')){echo '<span class="sale">'.$this->__('Sale').'</span>'; }?>
</div>
</li>
<?php if ($i%$_columnCount==0 || $i==count($_products)): ?>
</ul>
<?php endif ?>
<?php endforeach; ?>
</div>
</div>
<?php endif; ?>
<script>
jQuery(document).ready(function(){
jQuery('.btn-enquire').click(function(){
setTimeout(function(){ jQuery('.enquiry-btn').trigger('click') }, 1000);
return false;
});
});
</script>
after rearranging categories from back end it displays blank