This is my product collection.
$collectionWithRank = Mage::getResourceModel('catalog/product_collection')
->addCategoryFilter($cat_data)
->addAttributeToSelect('*')
->addAttributeToFilter('sort', ['neq' => ''])
->addAttributeToSort('sort', 'asc')
->addAttributeToSort('entity_id','desc');
$collectionWithRank->load();
$collectionWithoutRank = Mage::getResourceModel('catalog/product_collection')
->addCategoryFilter($cat_data)
// ->addAttributeToSelect(array('name', 'price', 'image', 'collect', 'url_key', 'sort'))
->addAttributeToSelect('*')
->addAttributeToFilter('sort', ['null' => true])
->addAttributeToSort('created_at', 'desc')
->addAttributeToSort('entity_id','desc');
$collectionWithoutRank->load();
$merged_ids = array_merge($collectionWithRankids, $collectionWithoutRankids);
$collection = Mage::getResourceModel('catalog/product_collection')
->addFieldToFilter('entity_id', array('in' => $merged_ids))
->addAttributeToSelect('*');
$collection->getSelect()->order(new Zend_Db_Expr('FIELD(e.entity_id, ' . implode(',', $merged_ids).')'));
if($sortby = "price_h_to_l"){
$collection->getSelect()->reset(Zend_Db_Select::ORDER);
$collection->addAttributeToSort('price', 'desc');
}
Above code affecting the product collection, where the price sort results are as below.
- Product 1 - $7000
- Product 2 - $6800
- Product 3 - $5000
- Product 4 - $200
- Product 5 - $4800
The order by is not as expected.
Can anyone help to sort out this?.
Mage::log($collection->getSelect()->__toString());
right after$collection->addAttributeToSort('price', 'desc');
and check the sort order of the statement?price_index
.min_price
desc". It's 'price' or 'min_price'?.