I want to sort the product collection by price. For this i am using
->setOrder('price', 'DESC') or ->addAttributeToSort('price', 'DESC')
is not working but it is working with ->addAttributeToSort('name','DESC')
perfectly.
$_productCollection = Mage::getModel('catalog/product')
->getCollection()
->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
->addAttributeToSelect('*')
->addAttributeToFilter('status', 1)
->addAttributeToSort('price', 'ASC')
->addAttributeToFilter('visibility', 4)
->addAttributeToFilter('category_id', array('in' => $categoryIds));
Best Answer
Try this add price to collection:
Or
Some Additional info:
setOrder() does not works
because of
As you have using setOrder('price', 'DESC') that it trying field to main table
catalog_product_entity
which does not exit that why it is not works.But whenever you use
addAttributeToSort()
function magento build automatically logic which properly call the fieldYou can the fundamental logic of
addAttributeToSort()
on classMage_Eav_Model_Entity_Collection_Abstract
That reason magento use AddAttributeToSort() to filter the product collection