InventoryUpdate Issue

700 views
Skip to first unread message

Suz Aann shrestha

unread,
May 14, 2018, 12:49:29 PM5/14/18
to Etsy API
   I am simply trying to update quantity and price with updateinventory Endpoints. What I did is :


   <?php 

                 function prepare_the_list_from_feed($id) {

                 $acc_req = $this->prepareHash($url,'GET',$type=false);
           
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $acc_req);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER , 1);
                curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
                $response = curl_exec($ch);
                curl_close($ch);
                $result = $response;
                $products = json_decode($result)->results;
                $products = $products->products;

               // Assumes products is the "products" portion of the above
               $products[0]->offerings[0]->quantity = 100;
        // Note we remove the entire price section and replace it with a string float.
        $products[0]->offerings[0]->price = '100.00';

        $data = array(
                'products' => json_encode($products),
        );

        $acc_req = $this->prepareHash($url,'PUT',$type=false);

        $ch = curl_init($acc_req);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
        curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($data));
        $response = curl_exec($ch);
        $result = $response;
        echo "<pre>";
        print_r($result);exit;

       ?>

the array of the product returned by etsy is  :
        
            Array
                (
                    [0] => stdClass Object
                        (
                            [product_id] => 2241471765
                            [sku] => 
                            [property_values] => Array
                                (
                                )

                            [offerings] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [offering_id] => 2353675953
                                            [price] => stdClass Object
                                                (
                                                    [amount] => 4200
                                                    [divisor] => 100
                                                    [currency_code] => USD
                                                    [currency_formatted_short] => $42.00
                                                    [currency_formatted_long] => $42.00 USD
                                                    [currency_formatted_raw] => 42.00
                                                )

                                            [quantity] => 1
                                            [is_enabled] => 1
                                            [is_deleted] => 0
                                        )

                                )

                            [is_deleted] => 0
                        )

                )

After this, the updated product data  is :

       Array
            (
                [0] => stdClass Object
                    (
                        [product_id] => 2241471765
                        [sku] => 
                        [property_values] => Array
                            (
                            )

                        [offerings] => Array
                            (
                                [0] => stdClass Object
                                    (
                                        [offering_id] => 2353675953
                                        [price] => 100.00
                                        [quantity] => 100
                                        [is_enabled] => 1
                                        [is_deleted] => 0
                                    )

                            )

                        [is_deleted] => 0
                    )

            )

But I am getting oauth_problem=signature_invalid&debug_sbs=POST&https%3A%2F%2Fopenapi.etsy.com%2Fv2%2Flistings%2F597363594%2Finventory&method%3DPUT%26oauth_consumer_key%3D****************%26oauth_nonce%3D********************%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D************%26oauth_token%3D*******************%26oauth_version%3D1.0

Oauth is working fine for listing, getting the listing by id, and also for variation endpoints (Old endpoints). But when I use Inventory Endpoints it gives me the above error. It is really frustating. Are Etsy developers are providing us any clue on this to solve our problems any sooner ? Any working example or code snippet would really help. Thanks.

etsyinventory.php

Faisal Pathan

unread,
Jul 11, 2018, 5:30:12 PM7/11/18
to Etsy API
Did you found any solution ?

sc...@luna-tide.com

unread,
Sep 11, 2018, 11:28:00 AM9/11/18
to Etsy API
Also getting this problem, and every forum thread I see on the internet is like this. Has anyone successfully done this? Does the API even work?

david olick

unread,
Sep 11, 2018, 2:48:00 PM9/11/18
to Etsy API V2
I am able to update the quantity/price.  The error message "signature_invalid" means you did not use oauth correctly - the message wasn't signed correctly (maybe you didn't sign the right data?).


--
You received this message because you are subscribed to the Google Groups "Etsy API" group.
To unsubscribe from this group and stop receiving emails from it, send an email to etsy-api-v2...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

bo...@greenchairpress.com

unread,
Sep 13, 2018, 2:19:33 PM9/13/18
to Etsy API
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)
  $data = $oauth->fetch("https://openapi.etsy.com/v2/listings/$oid/inventory", null, OAUTH_HTTP_METHOD_GET);
  $json = $oauth->getLastResponse();
                 # 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;
  }

Reply all
Reply to author
Forward
0 new messages