3

i'm using Magento ver. 1.5.0.1. In homepage i used 2 columns with left bar. i want to show latest,top rated and best products one below the other. please help me how to do this. i'm new to magento please help me....

Arasu
  • 2,078
  • 6
  • 40
  • 67

2 Answers2

13

In your app/design/frontend/{your-interface}/{your-theme}/template/catalog/navigation/left.phtml add the following code for latest products:

<?php

$_productCollection = Mage::getResourceModel('reports/product_collection')
                    ->addAttributeToSelect('*')
                    ->setVisibility(array(2,3,4))                   
                    ->setOrder('created_at', 'desc')
                    ->setPage(1, 5);
?>

<h2>Latest Products</h2>
<ul>
<?php foreach($_productCollection as $_product) : ?>
 <li><a href="<?php echo $_product->getProductUrl(); ?>"><?php echo $_product->getName(); ?></a></li>
<?php endforeach; ?>
</ul>

The top rated products are a bit more complicated. Use the following code:

<?php

$_productCollection = Mage::getResourceModel('reports/product_collection')
                   ->addAttributeToSelect('*')
                   ->setVisibility(array(2,3,4));

$_productCollection->joinField('rating_summary', 'review/review_aggregate', 'rating_summary', 'entity_pk_value=entity_id',  array('entity_type' => 1, 'store_id' => Mage::app()->getStore()->getId()), 'left');                
$_productCollection->setOrder('rating_summary', 'desc');
$_productCollection->setPage(1, 5);

?>

<h2>Latest Products</h2>
<ul>
<?php foreach($_productCollection as $_product) : ?>
 <li><a href="<?php echo $_product->getProductUrl(); ?>"><?php echo $_product->getName(); ?></a></li>
<?php endforeach; ?>
</ul>

Not sure what you meant by the best products but if bestsellers, here is the code for that:

<?php

$_productCollection = Mage::getResourceModel('reports/product_collection')
                   ->addAttributeToSelect('*')
                   ->setVisibility(array(2,3,4));

$select = $_productCollection->getSelect();

$sqlSelectColumns =  $select->getPart('columns');
$sqlSelectColumns[] = array(
            '',
            new Zend_Db_Expr('(
                SELECT SUM(order_item.qty_invoiced - order_item.qty_refunded)
                FROM ' . Mage::getSingleton('core/resource')->getTableName('sales/order_item') . ' AS order_item
                WHERE order_item.product_id = e.entity_id)
            '),
            'ordered_qty'
        );
$select->setPart('columns', $sqlSelectColumns);

$_productCollection->setOrder('ordered_qty', 'desc');
$_productCollection->setPage(1, 5);

?>

<h2>Top Selling Products</h2>
<ul>
<?php foreach($_productCollection as $_product) : ?>
 <li><a href="<?php echo $_product->getProductUrl(); ?>"><?php echo $_product->getName(); ?></a></li>
<?php endforeach; ?>
</ul>
user487772
  • 8,800
  • 5
  • 47
  • 72
  • thanks for answering and i added the new code in left.phtml and cleared the cache also but it doesn't shows anything but there are latest products. – Arasu Jun 09 '11 at 07:17
  • i updated the file app/design/frontend/base/default/template/catalog/navigation/left.phtml – Arasu Jun 09 '11 at 07:22
  • it worked but i changed in app/design/frontend/base/default/template/callouts/left_col.phtml. Thank You So much. – Arasu Jun 09 '11 at 07:36
  • 1
    The most complete answer for magento I ever saw on stackoverflow, not on magento.stackoverflow! Tx – John Mar 27 '15 at 14:24
  • please add and contribute to this question http://magento.stackexchange.com/questions/105698/magento-getting-store-bestsellers-configurable-products-only-from-current-stor and the code that is being created here https://github.com/seansan/SNH_Addorderqty – snh_nl Mar 11 '16 at 15:14
1

This extesnsion is very helpfull for your requirement. You can install this extension from url : http://www.magentocommerce.com/magento-connect/mageoutsourcing/extension/6669/bnm

After installation in your magento, a little chage into xml which helps to display best selling product in the left side of other. You should change layout xml from :

<?xml version="1.0"?>
<layout version="0.1.0">
    <default>
        <reference name="right">
            <block type="bnm/bestsellingproduct" name="bestsellingproduct.sidebar" after="cart_sidebar" template="bnm/bestselling-sidebar.phtml"/>          
        </reference>
    </default>    
</layout>

To :

<?xml version="1.0"?>
<layout version="0.1.0">
    <default>
        <reference name="left">
            <block type="bnm/bestsellingproduct" name="bestsellingproduct.sidebar" after="-" template="bnm/bestselling-sidebar.phtml"/>         
        </reference>
    </default>    
</layout> 

NOTE : before (and) after – These are two ways to position a content block within a structural block. before="-" and after="-" are commands used to position the block accordingly at the very top or very bottom of a structural block.

Sanjeev Kumar Jha
  • 1,213
  • 2
  • 12
  • 20