如何使用Shopify_api Ruby gem在Shopify中将所有产品设置为不可征税?
我需要使用如何使用Shopify_api Ruby gem在Shopify中将所有产品设置为不可征税?,ruby,shopify,Ruby,Shopify,我需要使用Shopify\u apiRuby-gem在Shopify中将10000多个产品变体设置为不可征税 我试过: irb(main):001:0> ShopifyAPI::Variant.update_all(taxable: false) 但我得到了错误信息: NoMethodError: undefined method `update_all' for ShopifyAPI::Variant:Class from (irb):1 from /var/lib/g
Shopify\u api
Ruby-gem在Shopify中将10000多个产品变体设置为不可征税
我试过:
irb(main):001:0> ShopifyAPI::Variant.update_all(taxable: false)
但我得到了错误信息:
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>'
NoMethodError:shopifigapi::Variant:Class的未定义方法“update\u all”
来自(irb):1
from/var/lib/gems/2.3.0/gems/shopify\u api\u console-2.0.0/lib/shopify\u api\u console/console.rb:156:in'launch\u shell'
from/var/lib/gems/2.3.0/gems/shopify\u api\u console-2.0.0/lib/shopify\u api\u 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_命令”
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'
来自/var/lib/gems/2.3.0/gems/shopify\u api\u console-2.0.0/bin/shopify api:4:in`'
from/usr/local/bin/shopify api:22:in'load'
from/usr/local/bin/shopify api:22:in`'
您可能需要迭代集合。我认为这应该是可行的,为了避免api速率限制,您可以每1000次调用就休眠一次
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
shopify\u应用程序
gem使用。
您可以查看其wiki以查看支持哪些方法(update\u all
不在其中)。据我所知(但我对使用Shopify还比较陌生),你不能批量更新10000个产品。有inventoryBulkAdjustQuantityAtLocation,但它仅用于库存
你必须打多个电话,还要注意shopify的
要更新产品变体,请尝试以下操作:
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
自2019-10年<代码>API版本起,您应按照以下方式对结果进行分页:
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
我认为这将更新产品及其所有变体。使用注释和。此代码更好
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
这个循环将不起作用,因为它在250个产品之后停止。要完成所有产品,您需要检查响应标题并提取分页参数,然后使用这些参数对产品进行分页。@DavidLazar感谢您的提醒!现在呢?那代码已经过时了,可能很快就会停止工作。您需要使用分页。调整分页只需很少的努力,您将受益于工作时间更长的代码@DavidLazar我已经更新了我的答案。你会这样做吗?不。。。看看我的答案。您的代码正在调用您已经获取的产品,以确保这将是超慢的。