I had problems getting it to work too. If the data isn't formatted exactly, the error message you get back is pretty confusing. I was finally able to get it to work using the code snippets in the documentation. Here's what worked for me. I use PHP & the oauth library -- same as the PHP in the docs. First updating the quantity, then an example of the price.
In the following test program, I get the inventory for a particular listing that I know has 1 variation with 2 options, and then set the quantity. I know that on all my listings, the price_on_property is 513 so it's hard-coded, but you can get that from the getInventory call if yours is different.
try {
# get the inventory for my listing ($oid)
# decode it into something I can update
$result = json_decode($json, true);
# update quantity for both options in my one variation (they have to be the same number for price_on_property)
$result['results']['products'][0]['offerings'][0]['quantity'] = 15;
$result['results']['products'][1]['offerings'][0]['quantity'] = 15;
# only send back what they expect -- the 'products' portion of the getInventory.
$params = $result['results']['products'];
# tell OAUTH to put the parameters as part of the
HTTP POST body for the next fetch
$oauth->setAuthType(OAUTH_AUTH_TYPE_FORM);
#now send the new quantities to etsy. I was only able to get this to work with this format -- having the array built in the fetch call
try {
$data = $oauth->fetch(
"
https://openapi.etsy.com/v2/listings/$oid/inventory",
[
'products' => json_encode($params),
'price_on_property' => 513,
'quantity_on_property' => '',
'sku_on_property' => ''
],
OAUTH_HTTP_METHOD_PUT,
array('Content-Type' => 'application/x-www-form-urlencoded'));
$json = $oauth->getLastResponse();
$result = json_decode($json, true);
exit;
}catch (OAuthException $e) {
error_log($e->getMessage());
error_log(print_r($oauth->getLastResponse(), true));
error_log(print_r($oauth->getLastResponseInfo(), true));
exit;
}
} catch (OAuthException $e) {
error_log($e->getMessage());
error_log(print_r($oauth->getLastResponse(), true));
error_log(print_r($oauth->getLastResponseInfo(), true));
exit;
}
To update the price on a listing or add an option for a variation, I found updating the getInventory return too cumbersome. So I just reinitialize the variations and options, without doing the getInventory first. In my test code that follows, my listing has 2 variations -- 'start month' and 'layout'. The price is on 'layout' -- which is 513. I have 7 layout options and 6 month options. This means I have 42 products (7x6).
$layout = [
"weekly pages only",
"monthly+weekly",
"weekly +envelope",
"weekly+marker",
"weekly/env/marker",
"month/week/marker",
"month/wk/marker/env",
];
$cost = [
"19.00",
"22.00",
"20.50",
"22.50",
"24.00",
"25.50",
"27.00",
];
$months = [
"August 2018",
"September 2018",
"October 2018",
"November 2018",
"December 2018",
"January 2019",
];
$products = [];
$toadd = 0;
# iterate over my 2 variations to get all the products. Start with the price_on_property (layout) -- didn't work doing the other order
for ($i=0; $i<count($layout); $i++) {
for ($j=0; $j<count($months); $j++) {
$prop2 = [
'property_id' => 514,
'property_name' => 'Start Month',
'values' => [$months[$j]],
];
$prop1 = [
'property_id' => 513,
'property_name' => 'Layout',
'values' => [$layout[$i]],
];
# $sku is from my local database and I need to set it in order to associate etsy listing ids with the items in my local product database
# I suspect it isn't required.
$products[$toadd++] = [
'sku' => $sku,
'property_values' => [$prop1, $prop2],
'offerings' => [
[
'price' => $cost[$i],
'quantity' => 10,
'is_enabled' => 1
]
]
];
}
}
$oauth->setAuthType(OAUTH_AUTH_TYPE_FORM);
try {
$data = $oauth->fetch(
"
https://openapi.etsy.com/v2/listings/$oid/inventory",
[
'products' => json_encode($products),
'price_on_property' => 513,
'quantity_on_property' => '',
'sku_on_property' => ''
],
OAUTH_HTTP_METHOD_PUT,
array('Content-Type' => 'application/x-www-form-urlencoded'));
$json = $oauth->getLastResponse();
$result = json_decode($json, true);
print_r($result);
}catch (OAuthException $e) {
error_log($e->getMessage());
error_log(print_r($oauth->getLastResponse(), true));
error_log(print_r($oauth->getLastResponseInfo(), true));
exit;
}