简体   繁体   English

使用一个API调用通过python更新shopify价格

[英]update shopify price via python with one API call

I'm working with the Shopify Python API, and I'd like to update the price of one of my products with a single API call. 我正在使用Shopify Python API,我想通过一个API调用来更新我的一种产品的价格。 This is because their API is throttled and I'm updating a large number of items, so a 50% reduction in API calls will significantly improve my overall running time. 这是因为他们的API受到限制,并且我正在更新大量项目,因此将API调用减少50%将大大缩短我的总体运行时间。

Right now I'm doing this: 现在我正在这样做:

product = shopify.Product.find(shopify_id)
product.variants[0].price = new_price
product.save()

This requires two API calls. 这需要两个API调用。 Is there a way to update a price, given a shopify ID, with a single API call? 在给定shopify ID的情况下,是否可以通过一个API调用来更新价格? I tried this (I was told on the forums that setting price on a Product would update all Variants): 我尝试了这一点(在论坛上告诉我,设置产品价格会更新所有变体):

product = shopify.Product(dict(id=shopify_id, price=new_price))
product.save()

and save() returned True, but the price did not update. 并且save()返回True,但是价格没有更新。 Then I tried this: 然后我尝试了这个:

product = shopify.Product(dict(id=shopify_id, price=new_price))
product.variants = [shopify.Variant()]
product.variants[0].price = new_price
product.save()

and save() returned False, product.errors.full_messages() returned ['Options are not unique']. 并且save()返回False,product.errors.full_messages()返回['选项不是唯一的']。

I know this is pretty old but it's a good question that was never properly answered. 我知道这已经很老了,但这是一个很好的问题,从未得到正确的回答。 It seems as though all the responses weren't exactly hearing what you were asking. 似乎所有答复都没有完全听到您的要求。

I have been working on a shopify app that requires updating many products at once which can take a while, especially if you need to call the API twice for each product (ie once to retrieve and again to update). 我一直在开发一个Shopify应用程序,该应用程序需要一次更新许多产品,这可能需要一段时间,尤其是如果您需要为每种产品两次调用API(即一次检索并再次更新)时。

I had to dig into the source code to find this but the product class has a add_variant method that seems to do the trick. 我必须深入研究源代码才能找到它,但是产品类具有一个add_variant方法,似乎可以解决问题。

product = shopify.Product(dict(id=123456789))
variant = shopify.Variant(dict(id=987654321, price=9.99))
product.add_variant(variant)
product.save()

This will update the product and allows you to basically perform a PUT request on the product. 这将更新产品,并允许您基本上对产品执行PUT请求。 You can update as many or as little variants as you want and it won't disturb the others. 您可以根据需要更新任意数量的变体,并且不会干扰其他变体。 Furthermore, it doesn't require the initial product call to retrieve the product you are updating. 此外,不需要初始产品调用即可检索要更新的产品。

Assuming you have the ID of the resource you want to update, just do a PUT call on it and you're good without first doing a GET. 假设您具有要更新的资源的ID,只需对其进行PUT调用即可,而无需先执行GET就可以了。 It is obvious that a product has no price, only variants do, hence your failure there. 显而易见,产品没有价格,只有变体有价格,因此您在那里失败。 Your other choice is to just suck it up and GET the product or variant resource, update the variant's price(s) and do a PUT. 您的另一选择是仅吸收它并获取产品或变体资源,更新变体的价格并进行PUT。 Note that API calls are cheap. 请注意,API调用很便宜。 Trying to save on them is often more painful than just burning a couple off. 试图节省下来的钱通常比烧掉一对夫妇要痛苦得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM