Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

How do I use setOrder()? 
 
Shpigford
Sr. Member
 
Avatar
Total Posts:  171
Joined:  2007-09-03
Denver, CO
 

I’m trying to pull a custom set of products and I’ve got this right now:

$products Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToFilter('status'1); //enabled
$products->addAttributeToFilter('visibility'4); //catalog, search
$products->addAttributeToSelect('*');
$products->setOrder('name''asc');

$prodIds=$products->getAllIds();

I then do a foreach:

foreach($prodIds as $productId{
      $product 
Mage::getModel('catalog/product');
      
$product->load($productId);
    
}

Unfortunately that setOrder() in the first bit of code doesn’t seem to do anything. What am I doing wrong?

(let me know if any other specifics are needed)

 
Magento Community Magento Community
Magento Community
Magento Community
 
mzentrale1
Guru
 
Total Posts:  731
Joined:  2007-12-06
Stuttgart, Germany
 

Hi,

this code works fine for me:

$collection Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter('status'1); //enabled
$collection->addAttributeToFilter('visibility'4); //catalog, search
$collection->addAttributeToSelect('name');
$collection->setOrder('name''ASC');
$products $collection->load();

echo 
"<pre>";
foreach(
$products AS $_product){
    
echo $_product->getName();
    echo 
"<br />";
}

cheers

Stefan

 
Magento Community Magento Community
Magento Community
Magento Community
 
Shpigford
Sr. Member
 
Avatar
Total Posts:  171
Joined:  2007-09-03
Denver, CO
 

Thanks so much Stefan. That does indeed work perfectly!

 
Magento Community Magento Community
Magento Community
Magento Community
 
g0rdian
Member
 
Total Posts:  65
Joined:  2011-12-04
 

Hi guys, apologies for bumping such an old thread.

How would I use setOrder() to sort ascending but placing NULL values last instead of first? Any idea if this is possible?

Thanks for any help on this one!

Reference: http://stackoverflow.com/a/9476900/1231001

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top