ruby on rails - Shopify API - How can i update product order on Smart Collection with thousands of products?
I am using Shopify's Ruby Gem to organize Smart Collections in a product store. Essentially I am trying to do a nightly update to the product order on these collections (their sort order is set to manual). The goal is to sort products based on sales data, but also put out of stock items at the end of the list.
I managed to write a script that works well on most collections:
@scs = ShopifyAPI::SmartCollection.find(:all,params: {limit: 250})
@scs.each do |sc|
sc.order(products: @products_in_order)
sc.save
end
Unfortunately if the collection is larger than a couple hundred products, the URI becomes too long and I get this error:
ActiveResource::ClientError: Failed. Response code = 414. Response message = Request-URI Too Large.
Is there any way to update product order on large smart collections in an efficient way? Some of my collections have upwards of 1000 products, and I do not want to have to update each "collect" individually as I will be running this script on a daily basis.
Any advice is appreciated.
Answer
Solution:
Your current technique is limited. The first thing you probably want to do is iterate all the collections by paging. Not paging means you'll never touch all the products in the collections with more than 250 products.
Second, as you page, per collection, build a hash with the details needed to sort them. Otherwise, you're storing a whole bunch of memory hungry hippos that will blow your memory footprint to hell. Use the hash to then order the products and save them.
And since this is scripting, and can run at night, suck it up and sort/store by product. Not like you're picking tomatoes or watermelons with your brute strength.
Source
Didn't find the answer?
Our community is visited by hundreds of Shopify development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.