0

I want to make a top offers block where special offer products with the highest percentage discount are shown.

I have my special offer products showing and just wondering if something like below is possible to sort these by their discount percentage:

$_productCollection->addFinalPrice()
          ->addMinimalPrice()
          ->getSelect()
          ->where('price_index.final_price < price_index.price')
          ->limit(2);
          ->order('(price_index.price - price_index.final_price) / price_index.price' , 'DESC')

Failing that i would like latest offers (sorting by special from date descending) which i think should be like so:

$_productCollection->addFinalPrice()
          ->addMinimalPrice()
          ->getSelect()
          ->where('price_index.final_price < price_index.price')
          ->limit(2);
          ->order('special_from_date' , 'DESC')

However this is not working. Has anyone any suggestions for how to use sort properly within Magento 2?

1 Answer 1

1

change your code

$_productCollection->addFinalPrice()
          ->addMinimalPrice()
          ->getSelect()
          ->where('price_index.final_price < price_index.price')
          ->limit(2);
          ->order('special_from_date' , 'DESC')

to

$_productCollection->addFinalPrice()
              ->addMinimalPrice()
              ->getSelect()
              ->where('price_index.final_price < price_index.price')
              ->limit(2);
              ->order('special_from_date DESC')
4
  • From what i can tell the below also works: ->order('((price_index.price - price_index.final_price) / price_index.price) DESC'); Thanks for your help! Not seen it like this on here however everything works now as expected.
    – harri
    Commented Jun 19, 2017 at 8:57
  • @harri can you please put your entire code here thanks in advance Commented Apr 3, 2019 at 10:03
  • @HarshJayswal create a thread and tag me. I will share the code. this is a different scenario.
    – Bojjaiah
    Commented Apr 4, 2019 at 4:48
  • @Bojjaiah I am new here can you please tell me how to create a thread or i have posted a question on magento community following is link to my question there i have shared my code magento.stackexchange.com/questions/268575/… Commented Apr 4, 2019 at 5:10

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.