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

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')
  • 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 Jun 19 '17 at 8:57
  • @harri can you please put your entire code here thanks in advance – Harsh Jayswal Apr 3 at 10:03
  • @HarshJayswal create a thread and tag me. I will share the code. this is a different scenario. – Bojjaiah Apr 4 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/… – Harsh Jayswal Apr 4 at 5:10

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.