13

This doesn't seem to be ordered correctly, anything I'm doing wrong? Suggestions?

$componentQuantityCollection = Mage::getModel('catalog/product')->getCollection();
$componentQuantityCollection->joinField('qty',
    'cataloginventory/stock_item',
    'qty',
    'product_id=entity_id',
    '{{table}}.stock_id=1',
    'left');
$componentQuantityCollection->addAttributeToFilter('sku', array('in' => $componentSkus))->setOrder('sku','ASC');

Another collection that doesn't seem to be sorted which is different than the first:

$kitCollection = Mage::getModel('kitinventory/kitinventory')->getCollection()->addFieldToFilter('kit_sku', $sku)->setOrder('related_sku', 'DESC');

5 Answers 5

47

EAV collections work with attributes, the sorting method is a little different here as well

$componentQuantityCollection->addAttributeToSort('sku', 'ASC');

For non-EAV collections use one of the following methods

$kitCollection->getSelect()->order('related_sku DESC');
$kitCollection->setOrder('related_sku', 'DESC');
7
  • what about the second collection? Mar 26, 2015 at 15:21
  • That's a flat type collection, so no EAV and attributes. Take a look at this answer on how to sort that: stackoverflow.com/a/11354060 Mar 26, 2015 at 15:23
  • I tried setOrder('related_sku','DESC'); but it's not sorted. Mar 26, 2015 at 15:26
  • I've edited my answer Mar 26, 2015 at 15:28
  • 2
    @easymoden00b use this $kitCollection->getSelect()->order('related_sku DESC');
    – Priyank
    Feb 28, 2017 at 13:10
15

You can add sort order like this:

$kitCollection->getSelect()->order('related_sku DESC');

More information: http://www.magentocommerce.com/wiki/1_-_installation_and_configuration/using_collections_in_magento

Hope can help you.

1
  • 3
    Fixed it for me, because i was using it like ->order('related_sku', 'desc'); Sep 25, 2015 at 15:05
5

To expand on the other answers here, $kitCollection->getSelect()->order('column DESC') works fine, but you can't add more than one column. For instance, $kitCollection->getSelect()->order('column DESC, column2 ASC') will error. This is because of the work that Magento does to escape the column names. To get around this, you can use a Zend_Db_Expr like so:

$kitCollection->getSelect()->order(new Zend_Db_Expr('related_sku DESC, column2 ASC'));
0
1

easymoden00b,setOrder() is not working due Eav structure on product.As @Sande say to use addAttributeToSort()function,because of

  • Magento is join multiple tables for product collection.

  • Attribute alias name at collection

  • setOrder() function is working when it order expression Fieldname,SortOrder is correct.

You can see,how magento is create field alias and it relate eav table attribute at class Mage_Eav_Model_Entity_Collection_Abstract

public function addAttributeToSort($attribute, $dir = self::SORT_ORDER_ASC)
{
    if (isset($this->_joinFields[$attribute])) {
        $this->getSelect()->order($this->_getAttributeFieldName($attribute).' '.$dir);
        return $this;
    }
    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';
            }
        }

        if (in_array($attrInstance->getFrontendClass(), $this->_castToIntMap)) {
            $orderExpr = Mage::getResourceHelper('eav')->getCastToIntExpression(
                $this->_prepareOrderExpression($orderExpr)
            );
        }

        $orderExpr .= ' ' . $dir;
        $this->getSelect()->order($orderExpr);
    }
    return $this;
}
0
1

Here is my solution for sorting the order of options in a configurable product's attribute. Start by copying Collection.php,

app/code/core/Mage/Catalog/Model/Resource/Product/Type/Configurable/Attribute/Collection.php to app/code/local/Mage/Catalog/Model/Resource/Product/Type/Configurable/Attribute/Collection.php.

Then you can find this code:

foreach ($this->getProduct()->getTypeInstance(true)->getUsedProducts(array($productAttribute->getAttributeCode()), $this->getProduct()) as $associatedProduct) {

And replace it with this code:

$assProds = $this->getProduct()->getTypeInstance(true)->getUsedProducts(array($productAttribute->getAttributeCode()), $this->getProduct());
sort($assProds);
foreach ($assProds as $associatedProduct) {

This will allow you to sort the drop-down list of attribute options to alphabetical. You can also reverse the order by using array_reverse() or similar functions.

Previously, my attribute options were in reverse alphabetical order. Now, they're in alphabetical order.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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