question

washington-l-braga-jr avatar image
washington-l-braga-jr asked washington-l-braga-jr answered

Best way to display a collection of products (regular and with variants)

Hi all,

I need to display a list of say 20 products. If any of those have variants I also have to display the proper name and prices for that item. Currently what I have is very slow when you receive 20 items that belong to an item group. You have to run 20 extra queries to fetch those item groups and all of its properties (N+1 problem).

Is that a more effective way to display a list of 20 products with all their info. Currently I have something like this:

collection = get("/merchants/#{mid}/items")
collection.each {
  get("/merchants/#{mid}/item_groups/#{item_group_id}") if has_item_group?
}

I've tried to use expand with itemGroup but the api seems to just ignore it and the api explorer also doesn't offer the itemGroup option. I've also tried to grab a collection of item group ids so I could fetch them all in one query but it doesn't seem to be possible either via api. Apparently filter only work with AND not OR.

I wonder how clover devices use the api for displaying a list of 10 full products for example (regular items and item with variants)? Is there any way to avoid those N+1? thanks appreciate it

10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

washington-l-braga-jr avatar image
washington-l-braga-jr answered

For the record ..

You can fetch multiple items by ID using the find param for example this would fetch item groups 2TDTAWDZ9GVMA and 7HXQVRSNM69SG:

get v3/merchants/{mid}/item_groups?find=id%3D2TDTAWDZ9GVMA&find=id%3D7HXQVRSNM69SG

10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Welcome to the
Clover Developer Community