Recently 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.