首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用shopify_api红宝石在购物中将所有产品设置为免税?

如何使用shopify_api红宝石在购物中将所有产品设置为免税?
EN

Stack Overflow用户
提问于 2019-12-08 21:33:29
回答 3查看 140关注 0票数 1

我需要在Shopify中使用shopify_api Ruby gem将10,000+产品变体设置为免税。

我试过了:

代码语言:javascript
复制
irb(main):001:0> ShopifyAPI::Variant.update_all(taxable: false)

但我得到了错误消息:

代码语言:javascript
复制
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>'
EN

回答 3

Stack Overflow用户

发布于 2019-12-09 01:51:04

shopify_app gem使用Active Resource。你可以查看它的wiki,看看它支持哪些方法(update_all不在其中)。据我所知(但我对使用Shopify还比较陌生)你不能批量更新10.000个产品。有inventoryBulkAdjustQuantityAtLocation,但它仅用于库存。

您必须进行多次呼叫,并且必须注意购物中心的rate limiting

要更新产品变体,请尝试执行以下操作:

代码语言:javascript
复制
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应用编程接口版本开始,您应该这样对结果进行分页:

代码语言:javascript
复制
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

我认为这将更新一个产品的所有变体。使用David's注释和post

票数 1
EN

Stack Overflow用户

发布于 2019-12-19 02:42:08

这个代码更好..。

代码语言:javascript
复制
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 
票数 1
EN

Stack Overflow用户

发布于 2019-12-09 01:29:28

你可能需要遍历一个集合。我认为这应该是可行的,为了避免api速率限制,您可以每1000个调用休眠一次。

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59235912

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档