I have something like this

foreach ($this->getProductCollection()as $prod) { ?>

And I want to reverse the foreach loop so that it starts with the last product in the array.

Is there a quick way to do this?

2 Answers 2


You should use addAttributeToSort() method of the Product Collection object before using it in a loop.

Something like that: //Correcting Misspellig, it was attAttribute $this->getProductCollection()->addAttributeToSort('attr_name', 'desc'); foreach ($this->getProductCollection() as $prod) { ... }

more about collections

  • 1
    Fatal error: Call to undefined method Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection::attAttributeToSort()
    – seb
    Commented Apr 1, 2015 at 9:44
  • 1
    it is addAttributeToSort not attAttributeToSort.
    – Shireen N
    Commented Apr 1, 2015 at 9:58

In place of addAttributeToSort you can also use setOrder('fieldname', direction [asc/desc])

Your Answer

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

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