I have the following piece of code:

$model = Mage::getModel('catalog/category')->load(1);
$collection = Mage::getResourceModel('catalog/product_collection');

foreach ($collection as $p ) {



This basically loops over the products within a category (with the ID of 1).

The strange behaviour I am experiencing is that the var_dump of the product ID varies in sort order depending where I call it from. I am calling it from two places: a controller and an observer (catalog_category_save_commit_after).

Is there anything I am missing? The ID's should be printed in the same order.

1 Answer 1


if you run these code echo "<pre>"; print_r($collection->getData()); you will come to know that there is no attribute with name product_id instead use entity_id.

so that your code becomes $collection->addAttributeToSort('entity_id','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.