1

I have a product collection. How does one go about sorting this collection the Magento Way?

1 Answer 1

5

easymoden00b,Magento have a magic function addAttributeToSort() which is sort your product Collection on basic of product attribute

Format:

$ProductCollectionObject->addAttributeToSort('attribute_code', 'sort_order')

Example:

$collection = Mage::getResourceModel('catalog/product_collection');
$collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
$collection->addAttributeToSort('created_at', 'desc');

Here i have filter the product collection by created_at attribute

3
  • this works -- although curiously it is only sorting the first 20 products of my catalog when I increase the amount displayed on the page (40, 60, etc). Jan 28, 2015 at 15:19
  • it also work for all limit of products .it may be cache issue
    – Amit Bera
    Jan 28, 2015 at 15:20
  • Why could be the reason that is not woking for me?...once I run the foreach, the loop starts since the first entity_id :/ Jan 27, 2018 at 1:23

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.