5

It my code when I sort product by price but result not show

$collection = $this->_productCollectionFactory->create();
$collection->addAttributeToSelect('*');
$collection->setOrder('price', 'DESC');

return $collection;
  • Please try before return $collection; echo $collection->getSelect()->__toString(); or echo $collection>printlogquery(true); – Ankit Shah Dec 7 '16 at 4:30
6

Use addAttributeToSort() function for sorting product collection by price.

So,

$collection->setOrder('price', 'DESC');

Change to

$collection->addAttributeToSort('price', 'DESC');
  • I want to sort my custom price after page load. @Amit – Dhaduk Mitesh Apr 13 '18 at 10:40

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.