So I\'ve Googled like crazy to try and find a solution to this problem that actually works properly but have come up empty handed.
When using the Sort By function on a
In response to Adam B's post.
My situation: I needed to sort products by a custom Magento (1.8.1.0) attribute with a numerical value. However, Magento was sorting like:
1 , 11 , 123 ,2 ,234, 3
(alphanumerical)
instead of: 1, 2, 3, 11, 123, 234
(numerical)
I honestly don't get why this is not working out of the box but I got this working with the following adaption:
$this->_productCollection->getSelect()->order(new Zend_Db_Expr("CAST( ".$filterAttribute." AS SIGNED ) ".$filterAttributeDir));
Hope it's to any use for someone.
For default sort order:
// Start of Code to force Magento to numerically sort decimal attributes rather than alphabetically
$filterAttribute = Mage::getBlockSingleton('catalog/product_list_toolbar')->getCurrentOrder();
//$filterAttribute = $this->getRequest()->getParam('order');
$filterAttributeDir = Mage::getBlockSingleton('catalog/product_list_toolbar')->getCurrentDirection();
//$filterAttributeDir = $this->getRequest()->getParam('dir');
So I found a thread on this on their documentation, and apparently it's a known pain point for the product. The best solution I found was to override the ORDER BY
for the query by calling a primitive method of the Collection
class, here's the example they give:
$_productCollection = Mage::getModel('catalog/product')->getCollection();
$_productCollection->setOrder(array('cm_brand', 'name', 'cm_length'), 'asc');
$_productCollection->getSelect()->reset(Zend_Db_Select::ORDER);
$_productCollection->getSelect()->order(array('cm_brand ASC', 'name ASC', 'CAST(`cm_length` AS SIGNED) ASC'));
Based on your example with only one sorting column, I would think you could go with:
$_productCollection = Mage::getModel('catalog/product')->getCollection();
$_productCollection->setOrder('weight', 'asc');
$_productCollection->getSelect()->reset(Zend_Db_Select::ORDER);
$_productCollection->getSelect()->order('CAST(`weight` AS SIGNED) ASC'));
Can you tell us the 'Catalog Input Type for Store Owner
'? Eg is it text or drop down? I mean, do you have a 'position' column in the Magento Admin (Catalog->Attributes->Manage attributes->Manage Label / Options->Manage Options (values of your attribute)). If your attribute is text, is the actual value the string '9kg'
or is it '9'
with 'kg'
added later?
Anyway, if you can set a position in the attribute editor then I think getSortOrder()
will help you (confirm this and someone can post an answer about getSortOrder()
).
If you cannot set a position (because the values are not pre-determined) then I think you will need to create the product sort order yourself* by perhaps stripping out the letters from the attribute values with a preg_replace()
or str_replace()
, sorting on the result of that and then passing the new sorted product array into the display loop - see 'rough pseudo code' in this answer.
*because I don't think any built-in generic sorting functions can tell the string '9kg'
is less than teh string '11kg'
or '3V'
is less than '18V'
**EDIT following comment:
Ah, yes, and I see Magento does not have a numeric input type for Catalog Input Type for Store Owner
. I think you should code your own sorting in the .phtml or the associated block php class (or if you know the weights in advance make it a dropdown eg 1,2,3,4... or 1.1, 1.2, 1.3...4.1, 4.2, 4.3...19.9) then you can tell Magento the position.
You might get away with the Date
input type or even the Fixed product tax
input type but I reckon that is asking for trouble (and a lot of testing).
Plan B might be to add some XML that defines a new numeric input type for Catalog Input Type for Store Owner
but I'd leave that to the core Magento developers - maybe they have a good reason for storing attribute values as strings.
If you are using the system attribute Weight
then that is fixed as input type text
and I think you have no choice other than coding your own sort.
I just found
//file: app/code/core/Mage/Core/Model/Locale.php
//class: Mage_Core_Model_Locale
//...
/*
* @param string|float|int $value
* @return float|null
*/
public function getNumber($value){
//...
Which could be useful. I don't think it will take you long to code your own sort.
Regarding the "Attribute with Numerical Value" situation above. I had the same problem and what I did was adding ceros "0" in front of my values.
For ex, my values were: 208, 209, 355, 1152 and 1153.
I added ceros: 00208, 00209, 00355, 01152 and 01153.
It works now!
This post also helped me:
http://blog.adin.pro/2014-04-30/magento-custom-sort-on-grid-sort-increment_id-by-numeric-not-alpha/
Hope this help!
Sorry, I'm a little late to the discussion. As I'm adverse to implementing code solutions when it isn't strictly necessary, I tried to think of an easier solution. I came up with the following.
Example problem sorting Original sorting order: 10.0", 10.5", 14.0", 8.0", 8.5"
Given that the list of numbers is sorted alphanumerically, I deduced that adding offsetting space characters (" ") before the 8s in my example above should result in the correct ordering. It did. This was the result.
Example correct sorting New sorting order: 8.0", 8.5", 10.0", 10.5", 14.0"
In the OP, Adam might have simple been able to replace "9kg" with " 9kg".
By extension, if the numbers in question range from ones values to hundreds values, the ones values would have 2 leading spaces, the tens values would have 1 leading space, etc.