Created
April 3, 2014 14:41
-
-
Save vovsky/9955612 to your computer and use it in GitHub Desktop.
example of sorting collection by multiple columns
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Atwix | |
* | |
* NOTICE OF LICENSE | |
* | |
* This source file is subject to the Open Software License (OSL 3.0) | |
* that is bundled with this package in the file LICENSE.txt. | |
* It is also available through the world-wide-web at this URL: | |
* http://opensource.org/licenses/osl-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 [email protected] so we can send you a copy immediately. | |
* @category Atwix Mod | |
* @package Atwix_Tweaks | |
* @author Atwix Core Team | |
* @copyright Copyright (c) 2014 Atwix (http://www.atwix.com) | |
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) | |
*/ | |
class Atwix_Tweaks_Model_Observer | |
{ | |
/** | |
* apply sorting in tagged product listing | |
* @param $event | |
*/ | |
public function catalogProductCollectionLoadBefore($event) | |
{ | |
/** @var Mage_Tag_Model_Resource_Product_Collection $collection */ | |
$collection = $event->getCollection(); | |
if (get_class($collection) == 'Mage_Tag_Model_Resource_Product_Collection') { | |
//adding news_from_date attribute to select doesn't work for some reason | |
$collection->addAttributeToSelect(array('created_at')); | |
$select = $collection->getSelect(); | |
if (!$collection->isEnabledFlat()) { | |
$attributeModel = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', 'news_from_date'); | |
$newsTableName = $attributeModel->getBackendTable(); | |
$select->joinLeft( | |
array('news_table' => $newsTableName), | |
new Zend_Db_Expr("e.entity_id = news_table.entity_id AND attribute_id = " . $attributeModel->getAttributeId() . " AND news_table.store_id = 0"), | |
array('news_table.value') | |
); | |
$select->columns(array('sorting_field' => new Zend_Db_Expr('IF(news_table.value IS NULL, e.created_at, news_table.value)'))); | |
} else { | |
$select->columns(array('sorting_field' => new Zend_Db_Expr('IF(news_from_date IS NULL, e.created_at, news_from_date)'))); | |
} | |
$select->order('sorting_field desc'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment