How can I add additional sort by: price, newest and most popular?
Thanks.
ce-1.7.0.2sorting
How can I add additional sort by: price, newest and most popular?
Thanks.
First add a dummy attribute called 'discount_percent' and make it "used for sorting" (just as Giuseppe said in the other answer).
It's not important what type it is or what values it has.
it's easier that way. If you don't want to add the attribute you have to rewrite the method Mage_Catalog_Model_Config::getAttributeUsedForSortByArray
to include your attribute. I would rather avoid rewrites as much as possible.
Now you need to tell magento to take a special action when the sorting is done by that attribute.
For this you have to rewrite (I don't know other way) the method Mage_Catalog_Model_Resource_Product_Collection::addAttributeToSort()
You will need a new module for that. let's call it Easylife_Sorting
.
Here are the files.
app/etc/module/Easylife_Sorting.xml
- the declaration file
<?xml version="1.0"?>
<config>
<modules>
<Easylife_Sorting>
<codePool>local</codePool>
<active>true</active>
<depends>
<Mage_Catalog />
</depends>
</Easylife_Sorting>
</modules>
</config>
app/code/local/Easylife/Sorting/etc/config.xml
- the configuration file
<?xml version="1.0"?>
<config>
<modules>
<Easylife_Sorting>
<version>1.0.0</version>
</Easylife_Sorting>
</modules>
<global>
<models>
<catalog_resource>
<rewrite>
<product_collection>Easylife_Sorting_Model_Resource_Product_Collection</product_collection>
</rewrite>
</catalog_resource>
</models>
</global>
</config>
app/code/local/Easylife/Sorting/Model/Resource/Product/Collection.php
- your class override. Here is where the magic happens.
class Easylife_Sorting_Model_Resource_Product_Collection
extends Mage_Catalog_Model_Resource_Product_Collection {
public function addAttributeToSort($attribute, $dir = self::SORT_ORDER_ASC){
//don't screw up the admin sorting
if (Mage::app()->getStore()->getId() == Mage_Core_Model_App::ADMIN_STORE_ID){
return parent::addAttributeToSort($attribute, $dir);
}
if ($attribute == 'discount_percent') {
//determine the discount percent value
$this->addExpressionAttributeToSelect('discount_percent_value', '(({{price}} - final_price) / {{price}})', array('price'));
//sort by the discount percent value
$sortDir = $dir;
//in case you need to reverse the order so when you sort ascending you will see the most discounts first do this:
//if not then remove the if-else statement below.
if ($sortDir == self::SORT_ORDER_ASC) {
$sortDir = self::SORT_ORDER_DESC;
}
else {
$sortDir = self::SORT_ORDER_ASC;
}
$this->getSelect()->order(array('discount_percent_value '.$sortDir));
return $this;
}
//otherwise do nothing special
return parent::addAttributeToSort($attribute, $dir);
}
}
This works for sure for simple/virtual/downloadable products. I have no idea what effect has on configurable/bundle and grouped products.
for -> Recently Viewed see here
for ->Sorting by rating
Copy the file
app/code/core/Mage/Catalog/Block/Product/List.php
to
app/code/local/Mage/Catalog/Block/Product/List.php
in list.php
find for this line
$this->_productCollection =$layer->getProductCollection();
which will be in around line no 86
add the following code after that
$this->_productCollection->joinField('rating_summary', 'review_entity_summary', 'rating_summary', 'entity_pk_value=entity_id', array('entity_type'=>1, 'store_id'=> Mage::app()->getStore()->getId()), 'left')
now copy
app/code/core/Mage/Catalog/Model/Config.php
to
app/code/local/Mage/Catalog/Model/Config.php
in config.php find for this code
$options = array(
'position' => Mage::helper('catalog')->__('Position')
);
replace with
$options = array(
'position' => Mage::helper('catalog')->__('Position'),
'rating_summary' => Mage::helper('catalog')->__('Rating')
);
-->>for BESTSELLER
follow this procedure create a folder naming Inchoo
and inside that folder place Catalog
and inside catalog create 3 folders Block
,etc
and Model
In Block
add Product
in Product
add List
and in List
create a file and name it as Toolbar.php
and ad this code into it
<?php
class Inchoo_Catalog_Block_Product_List_Toolbar extends Mage_Catalog_Block_Product_List_Toolbar
{
public function setCollection($collection)
{
parent::setCollection($collection);
if ($this->getCurrentOrder()) {
if($this->getCurrentOrder() == 'qty_ordered') {
$this->getCollection()->getSelect()
->joinLeft(
array('sfoi' => $collection->getResource()->getTable('sales/order_item')),
'e.entity_id = sfoi.product_id',
array('qty_ordered' => 'SUM(sfoi.qty_ordered)')
)
->group('e.entity_id')
->order('qty_ordered ' . $this->getCurrentDirection());
} else {
$this->getCollection()
->setOrder($this->getCurrentOrder(), $this->getCurrentDirection())->getSelect();
}
}
return $this;
}
}
now in etc
folder create a file with name config.xml
and add this code
<config>
<modules>
<Inchoo_Catalog>
<version>0.1.0</version>
</Inchoo_Catalog>
</modules>
<global>
<blocks>
<catalog>
<rewrite>
<product_list_toolbar>Inchoo_Catalog_Block_Product_List_Toolbar</product_list_toolbar>
</rewrite>
</catalog>
</blocks>
<models>
<catalog>
<rewrite>
<config>Inchoo_Catalog_Model_Config</config>
</rewrite>
</catalog>
<catalog_resource>
<rewrite>
<product_collection>Inchoo_Catalog_Model_Resource_Product_Collection</product_collection>
</rewrite>
</catalog_resource>
</models>
</global>
</config>
Now in Model
create a file naming Config.php
and add this code.
<?php class Inchoo_Catalog_Model_Config extends Mage_Catalog_Model_Config
{
public function getAttributeUsedForSortByArray()
{
return array_merge(
parent::getAttributeUsedForSortByArray(),
array('qty_ordered' => Mage::helper('catalog')->__('Sold quantity'))
);
}
}
also create Resource
folder in Model
and in Resource
folder create Product
folder and create a file naming Collection.php
and add following code.
<?php
class Inchoo_Catalog_Model_Resource_Product_Collection extends Mage_Catalog_Model_Resource_Product_Collection
{
protected function _getSelectCountSql($select = null, $resetLeftJoins = true)
{
$this->_renderFilters();
$countSelect = (is_null($select)) ?
$this->_getClearSelect() :
$this->_buildClearSelect($select);
if(count($countSelect->getPart(Zend_Db_Select::GROUP)) > 0) {
$countSelect->reset(Zend_Db_Select::GROUP);
}
$countSelect->columns('COUNT(DISTINCT e.entity_id)');
if ($resetLeftJoins) {
$countSelect->resetJoinLeft();
}
return $countSelect;
}
}
Now finally activate this module by going to app/etc/modules
create a file Inchoo_Catalog.xml
add this code.
<?xml version="1.0"?>
<!--
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE_AFL.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category Mage
* @package Mage_Connect
* @copyright Copyright (c) 2014 Magento Inc. (http://www.magentocommerce.com)
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/
-->
<config>
<modules>
<Inchoo_Catalog>
<active>true</active>
<codePool>community</codePool>
<depends />
</Inchoo_Catalog>
</modules>
</config>
and for SALE
I suggest you this extension as I cannot find any programmatic way to achieve this.
Best Answer
There are two ways to do it
1) Using Magento Admin Panel
2) Using Custom Coding
1) To add using Magento Backend you need to follow below steps
a) Goto Catalog->Attributes->Manage Attributes
b) Select the attribute you want to add in Sort By option. Suppose you need to add a Sort By Brand Option.
c) Goto Brand Attribute and check for "Used for Sorting in Product Listing".
d) Make it YES
Thats it you will see Brand attribute in Drop Down on product Listing page Sort by option.
2) Suppose you want to add Sort By option like Price: High-Low etc. These can't be done using attributes. For this you need to add a custom select option. Please check the link of my blog which explains how to do it Universal Coder
As per your question to sort the product by Newest product you need to follow below steps
1) Goto Catalog->Manage Attributes->Set Product as New from Date attribute
2) Mark it as Yes for sorting in product listing page
3) Now you need to add below custom code in
catalog->product->list->toolbar.phtml
this option is to be inserted under Select option for Sort By