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')
    ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
    ->addAttributeToFilter('status', 1)
    ->addAttributeToSort('price', 'ASC')
    ->addAttributeToFilter('visibility', 4)
    ->addAttributeToFilter('category_id', array('in' => $categoryIds));
  • what u r problem ->setOrder('price', 'DESC') no working or addAttributeToSort() not working – Amit Bera Sep 15 '15 at 7:44
  • Both are not working with price. – Chandresh Sep 15 '15 at 7:51

Try this add price to collection:




Some Additional info:

setOrder() does not works because of

Magent use Eav Structure for product attributes Whenever magento add an attribute to it product Collection then it join the collection with product attribute respective table (example catalog_product_varchar,catalog_product_ etc.)

And give an alias name to that attribute table and this attribute name in collection.

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 field

  if (isset($this->_staticFields[$attribute])) {
            $this->getSelect()->order("e.{$attribute} {$dir}");
            return $this;
        if (isset($this->_joinAttributes[$attribute])) {
            $attrInstance = $this->_joinAttributes[$attribute]['attribute'];
            $entityField = $this->_getAttributeTableAlias($attribute) . '.' . $attrInstance->getAttributeCode();
        } else {
            $attrInstance = $this->getEntity()->getAttribute($attribute);
            $entityField = 'e.' . $attribute;

        if ($attrInstance) {
            if ($attrInstance->getBackend()->isStatic()) {
                $orderExpr = $entityField;
            } else {
                $this->_addAttributeJoin($attribute, 'left');
                if (isset($this->_joinAttributes[$attribute])||isset($this->_joinFields[$attribute])) {
                    $orderExpr = $attribute;
                } else {
                    $orderExpr = $this->_getAttributeTableAlias($attribute).'.value';

You can the fundamental logic of addAttributeToSort() on class Mage_Eav_Model_Entity_Collection_Abstract

That reason magento use AddAttributeToSort() to filter the product collection

  • 1
    It's also not working. – Chandresh Sep 15 '15 at 12:39

I have solved this issue

$categoryIds = array($_category_id); //category id
                            $category = new Mage_Catalog_Model_Category();
                            $category->load($_category_id); // this is category id
                            $_productCollection = Mage::getModel('catalog/category')->load($_category_id)
                                                ->addAttributeToSelect('*') // add all attributes - optional
                                                ->addAttributeToFilter('status', 1) // enabled
                                                ->addAttributeToFilter('visibility', 4) //visibility in catalog,search
                                                ->setOrder('price', 'ASC'); //sets the order by price

You can try:

$productCollection = Mage::getModel('catalog/product')
    ->setOrder('price', 'ASC');

use set setOrder

$_productCollection = Mage::getModel('catalog/product')
    ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
    ->addAttributeToFilter('status', 1)
    ->setOrder('price', 'ASC');
    ->addAttributeToFilter('visibility', 4)
    ->addAttributeToFilter('category_id', array('in' => $categoryIds));
  • I had also tried ->setOrder('price', 'ASC'), it is also not working with price. – Chandresh Sep 15 '15 at 7:50

Easiest way for it is $this->_collection->getSelect()->order('min_price desc');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.