Magento Resource | E-Commerce Generation Explorer

Jun/09

29

How to sort/show products by sold quantity in Magento

Toolbar.php file location:
app/code/core/Mage/Catalog/Block/Product/List/Toolbar.php

Your _construct() function should be modified to look something like:

1
2
3
4
5
6
7
$this->_availableOrder = array(
‘position’ => $this->__(’Best Value’),
‘name’ => $this->__(’Name’),
‘price’ => $this->__(’Price’),
’special_price’ =>$this->__(’Special Price’),
‘ordered_qty’ => $this->__(’Quantity soled’),
);

Next we open the list.phtml inside /template/catalog/product/ folder. And you need to find the:

1
$_productCollection=$this->getLoadedProductCollection();


Expression and cut => paste somewhere to the top of the file. Then we add some additional logic to that same file. So you should write something like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Custom added
$orderFilterType = $this->getRequest()->getParam(’order’);
 
// Part of original template code, just moved to top of the file
$_productCollection=$this->getLoadedProductCollection();
 
// Custom added
if(isset($orderFilterType) && $orderFilterType === ‘ordered_qty’) {
$storeId = Mage::app()->getStore()->getId();
$_productCollection = null;
$_productCollection = Mage::getResourceModel(’reports/product_collection’)
->addAttributeToSelect(’*')
->addOrderedQty()
->setOrder(’ordered_qty’, $this->getRequest()->getParam(’dir’));
}

to the top of the list.phtml file.

File list.phtml should consists of two major parts:
•List mode, which starting with comment
•Grid mode, which starting with comment

At the beginning of each part (mode) there is a “foreach” loop like

1
<?php foreach ($_productCollection as $_product): ?>

What we need to do now is to duplicate entire content inside that foreach loop and set it in if-else statement where if statement would go like:

1
2
3
4
5
6
7
<?php if(isset($orderFilterType) && $orderFilterType === ‘ordered_qty’): ?>
// default template code between inside foreach loop
 
<?php else: ?>
// duplicated default template code inside foreach loo
 
<?php endif; ?>

Now do the same for Grid mode (since grid mode is the default one).

To test if the grid/list is returning the right stuff you might do something like

1
<?php echo(<!– product ‘. $_product->getName() .’ has been sold in qty:.$_product->ordered_qty.’ –>); ?>

inside the if statement.

· ·

6 comments

  • Chris King · 09/08/2010 at 16:59

    Hello,
    Great post there, I have been looking into this quite a bit over the past week or so and I have a couple of issues which perhaps you could discuss with me?
    I was able to implement your feature above BUT I was unable to get a value inside $orderFilterType in list.phtml (this could be just because I am using 1.4 however and this value needs to be called differently)
    However that is not the biggest concern. I can see that you creating a product collection inside list.phtml. This collection I am assuming is totally separate to the layered navigation? If so does that mean when you sort your category pages as so and then filter with the layered navigation the wrong product collection will be filtered? I am unsure if this would output as just the same product collection or switch back to the old one as I haven’t done it yet.

    Your thoughts on this would be great as I am struggling to come up with a solution on this.

    I look forward to your reply.

    Chris King

  • Nick · 19/10/2010 at 17:19

    Quick question.
    Using ordered_qty.’ Today’); ?> I would like to have it so that it will be restricted to a given time.
    $d = getdate(‘w’);
    From $d to $d

  • Nick · 19/10/2010 at 18:04

    Forgot to mention, I am trying to get this to work on the product page…

  • Pranali · 19/04/2011 at 08:27

    I tried the above code but when i select bestseller from the sort by drop down , and click on next page same products appear which are on the first page.Pagination is not proper.Please help.

  • mohamad mehdi habibi · 13/06/2011 at 10:27

    Great post there, thank you.

  • tuba · 19/03/2012 at 16:33

    Thank thank thank you man! 🙂

    Saved enough time to sort magento products!

Leave a Reply

<<

>>

Theme Design by devolux.nh2.me