1

Using below code, collection is filtered. If $productSkus array is like

Array ( [0] => 5714156000153 [1] => 8690604469871 [2] => 6412616365413 )

after filtering I see the collection is in

Array ( [0] => 8690604469871 [1] => 5714156000153 [2] => 6412616365413 )

sequence. How to do filtering with respect to given sequence?

$collection ->addAttributeToSelect('*')
            ->addAttributeToFilter('sku', array('in' => $productSkus));
0

You can try this way.

  $collection ->addAttributeToSelect('*')
              ->addAttributeToFilter('sku', array('in' => $productSkus))
              ->addAttributeToSort('sku'); 

It defaults to ASC and use addAttributeToSort('sku', 'desc') for DESC.

  • I'm asking for a solution to keep the sequence of given array. Your solution is sorting desc or asc. – Zinat Nov 8 '18 at 18:21
  • @Zinat did you find a solution for this to sort the products exactly as they are in the array? – paj Sep 17 at 12:15
  • @paj I had to keep a copy of original one, after filtering applied sorting based on that. – Zinat Sep 18 at 13:58
  • @zinat do you mean a copy of the original collection? Could you share your code with me please, I am struggling to achieve this. I am also losing the collection paging when I reorder it and see all products in the collection on the frontend. – paj Sep 18 at 14:07

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.