I am doing the following to take the vendor out of the products and using unique to get it to the front end to list each vendor on a store.
Controller:
@count = ShopifyAPI::Product.count
@n = 1
@products = ShopifyAPI::Product.find(:all, limit: 250, page: @n)
if (@count > 50) && params[:page_number]
@page_number = params[:page_number].to_i
@products = ShopifyAPI::Product.find(:all, params: {page: @page_number})
end
front end:
<% @products.map(&:vendor).uniq.each do |vendor| %>
...
...
<% end %>
<% if (@count > 50) && (@page_number.present? && @page_number > 1) %>
<%= link_to "<", company_shop_vendors_path(shop_id: @shop.id, page_number: (@page_number - 1)) %> <%= link_to "1", company_shop_vendors_path(shop_id: @shop.id, page_number: 1) %> <%= link_to "#{@page_number + 1} >", company_shop_vendors_path(shop_id: @shop.id, page_number: (@page_number + 1)) %>
<% elsif (@count > 50) %>
<%= link_to "#{@n+1} >", company_shop_vendors_path(shop_id: @shop.id, page_number: (@n += 1)) %>
<% end %>
This gets me the vendors but not how i would like.
The whole page system is a terrible attempt and I actually use a search form also which is better. paginating through the product pages just gets me the unique vendors on each page and when there are hundreds of pages, it’s pointless.
What is the best way to do this?
Is there maybe a way to paginate and throw all of the vendors into memory and THEN display to the front end all of the vendors uniquely?
Aisde from that, the only other option is to store the vendors in the database on a Product API request and then Product create webhook — instead of using the API directly.
What do you think?
3
Answers
Instead of using the API to display vendors --- and I am sure there is a way to do this while keeping the results in memory --- I cycle through the products and product pages and store the vendors in my database and display it that way. I allow users to "Sync" their vendors if they add new ones. I did not implement webhooks just in case someone makes a product with a new vendor that is mispelled or something.
You know Shopify has a Liquid construct where they will show you all the vendors of a shop?
https://help.shopify.com/en/themes/liquid/objects/shop#shop-vendors
So you can easily use that to send your App that listing, using an App Proxy. Is a heck of a lot more efficient than iterating products, and other hocus pocus.
Creepy Way but might help someone who doesn’t want to loop through products and won’t able to do through app proxy
This will list all product vendors in JSON format.
Might break the automate process. but can be helpful when you don’t want to loop through large data.
Hope the information will make sense and might help.