Create a file app/code/local/Mage/Catalog/Block/Product/Mostviewed.php 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 | 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(5)->setCurPage(1); $this ->setProductCollection( $products ); } } |
Create a file app/design/frontend/default/YourTheme/template/catalog/product/mostviewed.phtml 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 | <?php if (( $_products = $this ->getProductCollection()) && $_products ->getSize()): ?> <div class = " most_viewed" > <div class = "mv_title" ><?php echo $this ->__( 'These Products Are Popular Right Now!' ) ?></div> <?php $_collectionSize = 5; //count($_products->getItems()); echo $_collectionSize; ?> <ul class = "products-grid" id= "products-grid-table" > <?php $i =1; foreach ( $_products ->getItems() as $_product ): ?> <li id= "td_<?php echo $i;?>" <?php= "" if ( $i %5= "=0" or = "" $i = "=$_collectionSize){echo" 'class="last" ' ;}= "" ?>= "" > <div id= "cont_<?php echo $i;?>" > <a class = "product-image" href= "<?php echo $_product->getProductUrl() ?>" title= "<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" > <img alt= "<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" height= "135" src= "<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135); ?>" title= "<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" width= "135" > </a> <h3 class = "product-name" ><a href= "<?php echo $_product->getProductUrl() ?>" title= "<?php echo $this->htmlEscape($_product->getName()) ?>" ><?php echo $this ->htmlEscape( $_product ->getName()) ?></a></h3> <div class = "a-center" > <?php if ( $_product ->getRatingSummary()): ?> <?php echo $this ->getReviewsSummaryHtml( $_product , 'short' ) ?> <?php endif ; ?> <?php echo $this ->getPriceHtml( $_product , true) ?> <?php if ( $_product ->isSaleable()): ?> <button class = "button" onclick= "setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')" ><span><span><span><?php echo $this ->__( 'Add to Cart' ) ?></span></span></span></button> <div class = "clear" ></div> <?php else : ?> <p class = "availability" ><span class = "out-of-stock" ><?php echo $this ->__( 'Out of stock' ) ?></span></p> <div class = "clear" ></div> <?php endif ; ?> <ul class = "add-to-links" > <?php if ( $this ->helper( 'wishlist' )->isAllow()) : ?> <li><a href= "<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" ><?php echo $this ->__( 'Add to Wishlist' ) ?></a></li> <?php endif ; ?> <?php if ( $_compareUrl = $this ->getAddToCompareUrl( $_product )): ?> <li class = "last" ><span class = "separator" >|</span> <a href= "<?php echo $_compareUrl ?>" ><?php echo $this ->__( 'Add to Compare' ) ?></a></li> <?php endif ; ?> </ul> </div> </div> </li> <?php $i ++; endforeach ; $kol = $_collectionSize ; ?> </ul> </div> <?php endif ; ?> |
Now, we have the code in place which will fetch the most viewed products on call. Still, we need to add a block to show most viewed products in a desired location.
1 | {{block type="catalog/product_mostviewed" template="catalog/product/mostviewed.phtml"}} |
No comments:
Post a Comment