Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Page 1 of 4
best selling products/ most viewed
 
craigboi87
Sr. Member
 
Avatar
Total Posts:  90
Joined:  2008-07-18
 

I want to have several products on my homepage which are my best sellers or most viewed? Is it possible to do this so that it automatically updates depending on what is selling the best or viewed the most. How do I go about it??

Thanks in advanced

 
Magento Community Magento Community
Magento Community
Magento Community
 
craigboi87
Sr. Member
 
Avatar
Total Posts:  90
Joined:  2008-07-18
 

sorry but bump bump bump

 
Magento Community Magento Community
Magento Community
Magento Community
 
nikefido
Guru
 
Avatar
Total Posts:  481
Joined:  2008-07-11
New Haven, CT
 

there’s a thread hanging around somewhere that has a bestsellers module, but I’ll post the code I was successfully able to use to get “bestsellers” (based on that thread)- not sure about the “most viewed” part (altho if magneto keeps track of that metric, it would be easy to get)

This is the Model class you would use to get Bestsellers:

<?php
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''short_description''description')) //edit to suit tastes
            
->setStoreId($storeId)
            ->
addStoreFilter($storeId)
            ->
setOrder('ordered_qty''desc'); //best sellers on top
        
        
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
        
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);

        
//$products->setPageSize(6)->setCurPage(1);

        
$this->setProductCollection($products);
    
}
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
craigboi87
Sr. Member
 
Avatar
Total Posts:  90
Joined:  2008-07-18
 

and where do i put this code?

 
Magento Community Magento Community
Magento Community
Magento Community
 
craigboi87
Sr. Member
 
Avatar
Total Posts:  90
Joined:  2008-07-18
 

and where is thread too !  please

can any one help?????

 
Magento Community Magento Community
Magento Community
Magento Community
 
nikefido
Guru
 
Avatar
Total Posts:  481
Joined:  2008-07-11
New Haven, CT
 

Whoa, 3 weeks ago. I should start clicking the “Notify me via email” button....

OK, here is what I would do - you don’t need to declare a custom module for this (for the sake of simplicity)

The code above goes in “Bestseller.php”
app/code/local/Mage/Catalog/Block/Product/Bestseller.php

You then need a template to use.
I modified a stock Magento file (copied it and edited it)
app/design/frontend/*/*/template/catalog/product/bestseller.phtml
I used something like this:

<?php if (($_products $this->getProductCollection()) && $_products->getSize()): ?>
<div class="home-page-cntr">
<?php $i=0; foreach ($_products->getItems() as $_product): ?>
    <?php 
if ($i>5): continue; endif; ?>

<div class="home-page-item">
        <
div class="home-page-img">
            <
a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>">
                 <
img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(65,65); ?>" alt="<?php echo $this->htmlEscape($_product->getName()) ?>"/>
            </
a>
            
<?php echo $_product->getDescription(); //also getShortDescription ?>
        
</div>
        <
div class="home-page-txt">
            <
p><class="product-name" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>)"><?php echo $_product->getName() ?></a></p>
            
<?php echo $this->helper('review/product')->getSummaryHtml($_product'short'//product review link ?>
            
<?php echo $this->helper('catalog/product')->getPriceHtml($_product?>
            <?php 
echo $_product->getProductId(); ?>
            <?php 
if($_product->getevent_date()) {echo $_product->getevent_date();} ?>
            
</div>
</
div>
<?php $i++; endforeach; ?>
<?php 
for($i;$i%5!=0;$i++): ?>
    <?php 
endfor ?>
</div>
<?php endif; ?>

To use this code, you need to add the block somewhere, either via XML or in a CMS page like your home page:
To use in a CMS page, use this where you enter your content - use it where you want to this item to show up!:

{{block type="catalog/product_bestseller"  template="catalog/product/bestseller.phtml"}}
 
Magento Community Magento Community
Magento Community
Magento Community
 
nafnaf1000
Sr. Member
 
Total Posts:  209
Joined:  2008-02-21
 

If you look in the admin you will see that best sellers and Most Viewed are in the same format. maybe you just need to change the “Bestseller “ to “Most Viewed”

dos this code work in 1.1.3 a .4?

 
Magento Community Magento Community
Magento Community
Magento Community
 
nafnaf1000
Sr. Member
 
Total Posts:  209
Joined:  2008-02-21
 

@nikefido

I copied all your code and get this error message.

Fatal error: Class ‘Mage_Review_Helper_Product’ not found in ....app/code/core/Mage/Core/Model/App.php on line 779

 
Magento Community Magento Community
Magento Community
Magento Community
 
VimS
Jr. Member
 
Total Posts:  23
Joined:  2008-04-27
 

Hi!

Since I have upgraded to 1.1.6 it only shows a blank page.
On 1.1.4 it worked.

Any ideas what was changed?
I’ve found nothing in the release notes, that could help me!

 
Magento Community Magento Community
Magento Community
Magento Community
 
Vitor Braga
Jr. Member
 
Total Posts:  6
Joined:  2008-10-03
 

About this part of code: “<?php if ($i>5): continue; endif; ?>”, u can’t just use a limit in query?

 
Magento Community Magento Community
Magento Community
Magento Community
 
sparrovv
Jr. Member
 
Total Posts:  5
Joined:  2008-07-24
 

Anyone know why doesn’t it work with magento 1.1.6 ???

 
Magento Community Magento Community
Magento Community
Magento Community
 
jpbauer
Sr. Member
 
Total Posts:  159
Joined:  2008-10-09
Paris IDF
 

I’ve the same problem

 
Magento Community Magento Community
Magento Community
Magento Community
 
int2k
Sr. Member
 
Total Posts:  244
Joined:  2008-07-28
Bandung, Indonesia
 

change the code like this in 1.1.6

<?php if (($_products $this->getProductCollection()) && $_products->getSize()): ?>
<div class="home-page-cntr">
<?php $i=0; foreach ($_products->getItems() as $_product): ?>
    <?php 
if ($i>5): continue; endif; ?>

<div class="home-page-item">
        <
div class="home-page-img">
            <
a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>">
                 <
img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(65,65); ?>" alt="<?php echo $this->htmlEscape($_product->getName()) ?>"/>
            </
a>
            
<?php echo $_product->getDescription(); //also getShortDescription ?>
        
</div>
        <
div class="home-page-txt">
            <
p><class="product-name" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>)"><?php echo $_product->getName() ?></a></p>
            
<?php //echo $this->helper('review/product')->getSummaryHtml($_product, 'short') //product review link ?>
            
<?php echo $this->getReviewsSummaryHtml($_productfalsetrue)?>
            <?php 
//echo $this->helper('catalog/product')->getPriceHtml($_product) ?>
            
<?php echo $this->getPriceHtml($_product?>
            <?php 
echo $_product->getProductId(); ?>
            <?php 
if($_product->getevent_date()) {echo $_product->getevent_date();} ?>
            
</div>
</
div>
<?php $i++; endforeach; ?>
<?php 
for($i;$i%5!=0;$i++): ?>
    <?php 
endfor ?>
</div>
<?php endif; ?>
 
Magento Community Magento Community
Magento Community
Magento Community
 
shooby
Jr. Member
 
Total Posts:  11
Joined:  2008-10-31
Karlsruhe, Germany
 

Hi,

I managed it so that the products appear on the front page. what are the creteria that the products appear in this section?

and how do I set the outcome into a table so that everything is sorted? I also would like to delete the product information there. Just want to see the name and the price.

Can anybody help? I’m just into html and got no php skills.... yet.. smile

thanks in advance!

shooby

 
Magento Community Magento Community
Magento Community
Magento Community
 
tigreye007
Jr. Member
 
Total Posts:  1
Joined:  2009-01-20
 

Thanks to the above for the code - I’m a .NET guy, but I was able to get this working with the above steps with no PHP experience!

 
Magento Community Magento Community
Magento Community
Magento Community
 
Shven
Jr. Member
 
Total Posts:  12
Joined:  2008-10-25
 

tnx guys!
This is a well described method to put the bestsellers on a CMS page (homepage.)

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 4