In: Magento
9 Nov 2008Recently I wrote a post on Show products on sale in Magento. This one is somewhat of an upgrade to that post. In the previous post I mentioned, I had the case where I modified
app/code/core/Mage/Catalog/Block/Product/List/Toolbar.php
There I made some changes to the _construct() function. In order to implement another cool feature in our grid/list category product view I will make some additional modifications to Toolbar.php and the _construct() function.
Your _construct() function should be modified to look something like:
$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. You need to find the
$_productCollection=$this->getLoadedProductCollection();
expression and cut > paste somewhere to the top of the file. Then we add some additional logic to that same file. In general you should write something like
// 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 consists of two major parts:
At the beginning of each part (mode) there is a foreach loop like
<?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:
<?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
<?php echo(’<!– product ‘. $_product->getName() .’ has been sold in qty: ‘.$_product->ordered_qty.’ –>’); ?>
inside the if statement.
I avoid going into some details in this article since details are covered in the previous article for similar functionality. So if there is something here you do not understand, I suggest reading the Show product on sale in Magento.
One notice with this sorting by quantity soled functionality. It has one major drawback. If you find yourself (your store) using Configurable product approach, then you might want to implement some additional logic into the if statement since this approach returns soled quantities on single product approach. Therefore if you soled some configurable product 9 times it wont show only that quantity but the quantities of each product inside the configurable. This might sound confusing, hope you understood what I tried to say.
Hope you find this useful. Enjoy.
3 Responses to Sort (show) products by sold quantity in Magento
david
November 11th, 2008 at 5:51 pm
Hey thanks for the post. This is exactly what I need…hopefully I can get it to work.
I got the previous “special_price” post working, but for some reason there is no values for $_product->ordered_qty and ‘ordered_qty’ isn’t in $_product->debug(). For the special price, as you stated, it is included in this array if there is a special price set.
Is there something else I need to do to make ordered_qty included it in this array?
whoisgregg
December 4th, 2008 at 1:02 pm
Great code! Do you happen to know how to get it to only show products from a particular category or store?
When I add in that code, it shows every single product from all the stores running on this particular installation… not ideal.
Bivek
December 9th, 2008 at 3:50 am
Please see this post and reply:
http://www.magentocommerce.com/boards/viewthread/25393/
Thnk you