Update product price via API

Solved
Highlighted
Tourist
13 3 8

Hey everyone - very quick question.  How do you update a product's price via the API? Just including a "price" key/value in the PUT/POST payload isn't doing the trick.  Do I have to create a product variant for this, even if my product has no variants?

 

Thanks for your help!

0 Likes
Highlighted
Shopify Partner
397 28 61

Hi @anonymous1 ,

 

Use your product ID for variant ID see if that works. Also wouldn't hurt to show us what you're payload and any errors.

 

Regards,

 

Sam

Custom Apps and Theme Support | Email us: hello@achieveapplabs.com | Follow us on Twitter @achieveapplabs | Hosted solutions | Lifetime support
0 Likes
Highlighted
Tourist
13 3 8

Thanks!  I guess that's my question - do you have to update a "variant" to update price?  The docs are surprisingly silent about what I would think would be a top-level  operation for any Shopify dev - updating price via API.

 

Here's what my payload looks like to the /products endpoint:

product_payload = {
    'product': {
        'title': self.title,
        'body_html': self.description,
        'published': self.published,
        'price': json.dumps(self.price, use_decimal=True)
    }
}

Everything but price gets updated/set correctly.

 

Thanks for your time!

0 Likes
Highlighted
Tourist
13 3 8

This is an accepted solution.

I want to respond to my own thread here with the solution.  A thing that is not 100% clear about Shopify products is that they DO NOT have a price associated with them.  Only the product VARIANTS have prices.  Even if you have a product with no variants, Shopify has secretly created a single variant for your product and this is the object that contains your price.  So if you wanted to update your product's price, you can't make a call to /products/{product.id}.json you have to call /variants/{variant.id}.json with price as part of the payload.  A bit confusing and not exactly spelled out, but makes sense!

Highlighted
Tourist
10 0 0

Thanks for the answer, but It is really a strange that for a product without any variant you update /variants/{variant.id}.json instead of /products/{product.id}.json.

Anyway Good Catch.

0 Likes
Highlighted
Shopify Partner
397 28 61

 remember running into this and yes the docs didn’t make it clear what ID to use when a product has no variants. My vote towards enhancing the document for Orders API.

Custom Apps and Theme Support | Email us: hello@achieveapplabs.com | Follow us on Twitter @achieveapplabs | Hosted solutions | Lifetime support
0 Likes
Highlighted
Tourist
10 0 0

@achieveapplabs Agreed.

0 Likes
Highlighted
New Member
10 0 0
var data = JSON.stringify({
"product": {
"title": "Test",
"body_html": "Test",
"vendor": "Test",
"product_type": "Test",
"variants": [
{
"title": "Test1",
"price": "500"
}
]
}
});

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
console.log(this.responseText);
}
});

xhr.open("PUT", "https://myshopify.com/admin/products/5192344207495.json");
xhr.setRequestHeader("x-shopify-access-token", "privatekey");
xhr.setRequestHeader("host", "lonely-walls-inc.myshopify.com");
xhr.setRequestHeader("content-type", "application/json");
xhr.setRequestHeader("cache-control", "no-cache");
xhr.setRequestHeader("postman-token", "17fb6a1b-1763-3646-21a1-409e1c23b22c");

xhr.send(data);
0 Likes