我需要在Shopify中使用shopify_api Ruby gem将10,000+产品变体设置为免税。
我试过了:
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>'发布于 2019-12-09 01:51:04
shopify_app gem使用Active Resource。你可以查看它的wiki,看看它支持哪些方法(update_all不在其中)。据我所知(但我对使用Shopify还比较陌生)你不能批量更新10.000个产品。有inventoryBulkAdjustQuantityAtLocation,但它仅用于库存。
您必须进行多次呼叫,并且必须注意购物中心的rate limiting。
要更新产品变体,请尝试执行以下操作:
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应用编程接口版本开始,您应该这样对结果进行分页:
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发布于 2019-12-19 02:42:08
这个代码更好..。
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 发布于 2019-12-09 01:29:28
你可能需要遍历一个集合。我认为这应该是可行的,为了避免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
endhttps://stackoverflow.com/questions/59235912
复制相似问题