Please help me I want to see the products in Search Results order by ordered quantity means which products are more sold those products shows at first Thanks in
-
which magento version are you using?– Aboodred1Apr 28, 2014 at 21:37
-
Magento ver. 1.13.1.0...i am using enterprise edition– lathaApr 29, 2014 at 5:46
-
Actually i added custom code in Layer.php in public function prepareProductCollection($collection) added these few lines $collection->getSelect() ->joinLeft( 'sales_flat_order_item AS order_item', 'e.entity_id = order_item.product_id', 'SUM(order_item.qty_ordered) AS ordered_qty') ->group('e.entity_id') ->order('qty_ordered DESC'); now i got the products list ordere by ordered quantity but here have 2 problems 1 is pagination and result count shows as 1 every result.. Please help me....– lathaApr 29, 2014 at 7:19
1 Answer
One way of solving your issue is; by adding additional sort option like Relevance/Position sort option. In order for you to add a "ordered quantity" option you need to do the following:
- First copy
Config.php
fromapp/code/core/Mage/Catalog/Model/Config.php
toapp/code/local/Mage/Catalog/Model/Config.php
and replace this functiongetAttributeUsedForSortByArray()
with the following code:
public function getAttributeUsedForSortByArray()
{
$options = array(
'position' => Mage::helper('catalog')->__('Position'),
'ordered_qty' => Mage::helper('catalog')->__('Ordered Qty'),
);
foreach ($this->getAttributesUsedForSortBy() as $attribute) {
/* @var $attribute Mage_Eav_Model_Entity_Attribute_Abstract */
$options[$attribute->getAttributeCode()] = $attribute->getStoreLabel();
}
return $options;
}
- Second copy
Toolbar.php
fromapp/code/core/Mage/Catalog/Block/Product/List/Toolbar.php
toapp/code/local/Mage/Catalog/Block/Product/List/Toolbar.php
and replace this functionsetCollection()
with the following code:
public function setCollection($collection)
{
$this->_collection = $collection;
$this->_collection->setCurPage($this->getCurrentPage());
// we need to set pagination only if passed value integer and more that 0
$limit = (int)$this->getLimit();
if ($limit) {
$this->_collection->setPageSize($limit);
}
if ($this->getCurrentOrder()) {
if($this->getCurrentOrder() == 'ordered_qty') {
$this->getCollection()->getSelect()
->joinLeft('sales_flat_order_item AS so', 'e.entity_id = so.product_id', 'SUM(so.qty_ordered) AS ordered_qty')
->group('e.entity_id')->order('ordered_qty ' . $this->getCurrentDirection());
} else {
$this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
}
}
return $this;
}
- Third to set "Ordered Qty" sort option as default for Search Result and Advance Search Result add/edit
local.xml
under your active theme folderapp/design/frontend/your_package/your_theme/layout/local.xml
and add the following:
<?xml version="1.0" encoding="UTF-8"?>
<layout>
<catalogsearch_result_index>
<reference name="search_result_list">
<action method="setDefaultDirection"><param>desc</param></action>
<action method="setSortBy"><param>ordered_qty</param></action>
</reference>
</catalogsearch_result_index>
<catalogsearch_advanced_result>
<reference name="search_result_list">
<action method="setDefaultDirection"><param>desc</param></action>
<action method="setSortBy"><param>ordered_qty</param></action>
</reference>
</catalogsearch_advanced_result>
</layout>
I hope this will help you.
-
Hi..Thanks for your reply... i don't want to show..orders list at front end ..in my app that front option is not there.... I want to enable that order in search result page, got that page path i.e app/code/core/Mage/catalogsearch/model/Layer.php Pls help me.....– lathaApr 30, 2014 at 17:53
-
@latha with my solution, hiding ordered qty option from frontend is considered an option for you? Apr 30, 2014 at 18:05
-
-
thanq...so much.... It's very nice... you save my time... Thanq very much... I done with only second step...it's working...nice answer..it's save my time...– lathaMay 1, 2014 at 7:40
-
@latha glad it worked for you :D ... feel free to market it as correct answer if you will May 1, 2014 at 14:06