0

From a category page, I grab the product collection from the category object like so

$productCollection = $category->getProductCollection()->addAttributeToSelect('*');

However, the results are sorted in the reverse order of how they appear on the category page. The products on the category page are sorted in the same way they are in the Magento admin (which I assume is simply 'date added to category').

Is there a way I can use

$productCollection->setOrder() 

In a way that returns my collection in the same order as they appear on the category page?

4

You can filter product collection by updated date or create date DESC/ASC order.

$productCollection->addAttributeToSort('update_at', 'ASC');
$productCollection->addAttributeToSort('created_at', 'DESC');
1
  • Perfect, thanks. In my case addAttributeToSort('created_at', 'DESC') did it, which I assume is the way they are sorted by default somewhere in Magento – jaybong Sep 26 '19 at 12:55
3

You can try $productCollection->setOrder('position','ASC'); to load collection by position in ASC order.

You can use different parameters in the setOrder() for example entity_id, created_at, updated_at etc and ASC and DSC for their appearing order.

1
  • 1
    Thanks, along with the answer from sangeeta this also worked, specifically: setOrder('created_at','DESC') matched the order of the products on the category. – jaybong Sep 26 '19 at 13:00

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.