1

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

4

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

  • 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). – easymoden00b Jan 28 '15 at 15:19
  • it also work for all limit of products .it may be cache issue – Amit Bera Jan 28 '15 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 :/ – Beto Castillo Jan 27 '18 at 1:23

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.