0

I have a collection of products which I filter and show out of stock items only and it works fine. This is what I have:

$collection = Mage::getResourceModel('reports/product_collection')
    ->addAttributeToSelect('*');
$collection->joinField('qty','cataloginventory/stock_item','qty','product_id=entity_id','{{table}}.stock_id=1','left')
    ->addAttributeToFilter('qty', array('eq' => 0));

I wanted to sort by Price so I added this:

$collection->setOrder('price', 'desc');

Unfortunately this does not work. and I get a blank page.

If I change the Filter on the Join to show in stock products it works and will show in the correct order

->addAttributeToFilter('qty', array('neq' => 0));

What am I missing?

Update:

I removed the filter completely and sorted the collection and it only shows in stock products. It refuses to show out of stock products after sorting.

Update:

If I show out of stock products on front end it works:

Admin >> System >> Configuration >> Catalog >> Inventory >> Stock Options
Display Out of Stock product -> Yes

I do not want to show out of stock products on the front end though, only in this collection.

0

if your setOrder not working then you can try below code as well

$collection->getSelect()->order('price', 'desc');

Also for EAV attribute you can use

 $collection->addAttributeToSort('attribute_code', 'ASC'); //or desc as per your requirement 
  • This didn't work it gave the error: THERE HAS BEEN AN ERROR PROCESSING YOUR REQUEST – Mike Tim Turner Mar 22 '17 at 10:25

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.