Skip to content

Instantly share code, notes, and snippets.

@colinmollenhour
Created November 15, 2012 23:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save colinmollenhour/4082426 to your computer and use it in GitHub Desktop.
Save colinmollenhour/4082426 to your computer and use it in GitHub Desktop.
Fix Magento product list sort order for 'select' type attributes.
--- a/app/code/core/Mage/Catalog/Block/Product/List.php
+++ b/app/code/core/Mage/Catalog/Block/Product/List.php
@@ -157,6 +157,19 @@ class Mage_Catalog_Block_Product_List extends Mage_Catalog_Block_Product_Abstrac
// set collection to tollbar and apply sort
$toolbar->setCollection($collection);
+ // Redo order to sort based on attribute label sort order
+ if ($order = $toolbar->getCurrentOrder()) {
+ $eavConfig = Mage::getSingleton('eav/config'); /* @var $eavConfig Mage_Eav_Model_Config */
+ $attribute = $eavConfig->getAttribute('catalog_product', $order);
+ if ($attribute && $attribute->getFrontendInput() == 'select' && $attribute->getSourceModel() == 'eav/entity_attribute_source_table') {
+ $collection->getSelect()->reset(Zend_Db_Select::ORDER);
+ $dir = strtoupper($toolbar->getCurrentDirection());
+ $collection->getSelect()
+ ->joinLeft('eav_attribute_option AS eao', "eao.option_id=IF({$order}_t2.value_id > 0, {$order}_t2.value, {$order}_t1.value)", array())
+ ->order(new Zend_Db_Expr('eao.sort_order '.$dir));
+ }
+ }
+
$this->setChild('toolbar', $toolbar);
Mage::dispatchEvent('catalog_block_product_list_collection', array(
'collection' => $this->_getProductCollection()
@liudaz
Copy link

liudaz commented Nov 23, 2012

Hi, Mate. Thanks for the patch.
Just checking your solution - but am receiving an SQL error.
Where does this come from: {$order}_t2?
The *_t2 table?

Thanks.

@ytm2k
Copy link

ytm2k commented Jan 12, 2014

Hey,

Any update to the above? when I apply the patch and try to sort everything just hangs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment