No sorting in Product API

Hi!
I need need to return a list of products to my front-end through the API, but I need the products in the same order I pass them. For example, if I do myhost/api/products?pid=5,2,3,1 the API returns the products through alphabetical order or by product_id or code or price, but I need to return the products in the same order as I passed them, any idea how could I do this?

Please try

myhost/api/products?item_ids=5,2,3,1

Hi, thanks for your answer, it worked as expected!

1 Like

As I can no longer edit the post, Im posting again:
I Seem to be some troubles when adding pagination to this, as the pages come out unordered or with items from other pages
http://localhost/api/products?item_ids=1074 1070, 1124, 1156, 1058, 1109?&extend[]=description&extend[]=full_description&amount_from=1&items_per_page=3&page=2&sort_by=null
returns something along the lines of 1058, 1156, 1109 or 1158 and 1109. but the the desired “1156, 1058, 1109”

Making some more test, seems like it does not even display the wanted solution anymore, I dont think I did anything of importance, only tested with queries like page and items_per_page
Any ideas?

Please try

api/products?item_ids=1074,1070,1124,1156,1058,1109&extend[]=description&extend[]=full_description&amount_from=1&items_per_page=3&page=2

Thanks for your answer, unfortunately, still getting things out of order, with this call:
/api/products?item_ids=1074,1070,1124,1156,1058,1109&extend[]=description&extend[]=full_description&amount_from=1&items_per_page=3&page=2

Im getting the other 1070, 1124, 1109.
I should be getting 1156, 1058, 1109
Best regards

Maybe I don’t understand the post fully, but have you tried adding parameters sort_by and sort_order to the query? Or this is not sufficient for your needs?

Not sufficient, as what I need is not ordering at all, just getting the products in the order of the ID’s I pass to the end point and this is important.
For example, in that last call I would expect to get in page 1 the products
1074,1070,1124
and in page 2 1156,1058,1109
This is a small example, but I hope you understood what Im trying to achieve.

I am afraid it is impossible in the CS-Cart by default. You will have to add additional sorting via custom modification, to achieve the desired functionality,

Thanks for your reply, guess we will have to paginate ourselves in our storefront then.

2 Likes

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.