Reputation: 59
I need to set 10,000+ product variants to untaxable in Shopify using the shopify_api
Ruby gem.
I have tried:
irb(main):001:0> ShopifyAPI::Variant.update_all(taxable: false)
but I get the error message:
NoMethodError: undefined method `update_all' for ShopifyAPI::Variant:Class
from (irb):1
from /var/lib/gems/2.3.0/gems/shopify_api_console-2.0.0/lib/shopify_api_console/console.rb:156:in `launch_shell'
from /var/lib/gems/2.3.0/gems/shopify_api_console-2.0.0/lib/shopify_api_console/console.rb:113:in `console'
from /var/lib/gems/2.3.0/gems/thor-0.18.1/lib/thor/command.rb:27:in `run'
from /var/lib/gems/2.3.0/gems/thor-0.18.1/lib/thor/invocation.rb:120:in `invoke_command'
from /var/lib/gems/2.3.0/gems/thor-0.18.1/lib/thor.rb:363:in `dispatch'
from /var/lib/gems/2.3.0/gems/thor-0.18.1/lib/thor/base.rb:439:in `start'
from /var/lib/gems/2.3.0/gems/shopify_api_console-2.0.0/bin/shopify-api:4:in `<top (required)>'
from /usr/local/bin/shopify-api:22:in `load'
from /usr/local/bin/shopify-api:22:in `<main>'
Upvotes: 1
Views: 222
Reputation: 11427
This code is better...
products = ShopifyAPI::Product.find(:all)
process_products(products)
while products.next_page?
products = products.fetch_next_page
process_products(products)
end
def process_products(products)
products.each do |product|
# do something with product
end
rescue StandardError => e
puts "Process failed #{e.message} #{e.backtrace}"
end
Upvotes: 1
Reputation: 180
shopify_app
gem uses Active Resource
.
You can check its wiki to see what methods are supported (update_all
is not one of them). As far as I know (but I am relatively new to using Shopify) you can not bulk update 10.000 products. There is inventoryBulkAdjustQuantityAtLocation, but it is for inventory only.
You have to make multiple calls and have to mind shopify's rate limiting.
To update product variants, try this:
page = 1
count = ShopifyAPI::Product.count
# Get total number of products
if count > 0
page += count.divmod(250).first
while page > 0
products = ShopifyAPI::Product.find(:all, params: {limit: 250, page: page})
products.each do |p|
product = ShopifyAPI::Product.find(p.id)
product.variants.each do |v|
v.taxable = false
end
product.save
end
page -= 1
end
end
As of 2019-10
API version this is how you should paginate results:
products = ShopifyAPI::Product.find(:all, params: { limit: 50 })
while products.next_page?
products = products.fetch_next_page
products.each do |p|
product = ShopifyAPI::Product.find(p.id)
product.variants.each do |v|
v.taxable = false
end
product.save
end
end
I think this will update a product with all its variant. Using David's comment and post.
Upvotes: 1
Reputation: 11186
You probably need to iterate over a collection. I think this should work, and to avoid api rate limit you can sleep every 1000 calls.
variants = ShopifyAPI::Variant.find(:all)
variants.each_with_index do |v, call_counter|
if call_counter % 1000 == 0
sleep 1 # sleep 1 second every 1000 calls
end
v.taxable = false
v.save
end
Upvotes: 0