简体   繁体   中英

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. 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.

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. Is there a way to update a price, given a shopify ID, with a single API call? 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. 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'].

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).

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.

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. 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. 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. Note that API calls are cheap. Trying to save on them is often more painful than just burning a couple off.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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