In February we showed you how to integrate stock sharing with eBay, now we get to grips with Amazon’s API to do the same.

If you’ve implemented eBay stock sharing then prepare for a bit more work here as there are a few more obstacles in the way with this integration (including the terrible documentation) as Amazon throttle how much data you can send to, and receive from, them. In this post we are only providing the code to update stock; you will need to define the processes around it to adhere to Amazon’s rules.

Updating Amazon

I recommend storing all stock sold in a [stock sold] table then running through it every 20 minutes, pulling 15 products out at a time to inform Amazon to update. This means you will stay within Amazon’s throttle limits. It does of course mean that your stock levels won’t be 100% accurate all the time. Thanks Amazon.

First we need to assign the relevant details to the following variables (use the ones Amazon has given you), set up some parameters and place them in an array to be used later on.

Note the variables you’ve already populated, such as your AWS access key. The “FeedType” tells Amazon which of their many feeds we are wanting to use.

Then, looping through each product that needs to be updated we build up the “messages”. Each message tells Amazon which product we want to update based on its SKU. The MessageID needs to be unique for each message in the call (I just start at 1 and increment by one for each message). Quantity is the amount by which to reduce the stock i.e. how many have been sold.

Then we incorporate the message into the complete XML.

Just a bit more messing around here, setting up the headers and incorporating the $url array we created earlier.

And finally we get to actually send the XML to Amazon, using cURL.

If the almost-impossible-to-find $result is “_SUBMITTED_” then it’s worked, hurray! If not, good luck.

I would then update the [stock sold] table so we know which products have been updated and we don’t attempt to update them again.

Updating Website

Now we can update Amazon when we’ve sold something on our site, let’s see how we update our website when something’s sold on Amazon.

We need to import orders. For this we need the help of Amazon’s PHP SDK, MarketplaceWebServiceOrders, which you can find here (note: at time of writing this is the latest version, if a new version is out it should be linked to from here).

Firstly you’ll need to edit the config file (config.inc.php) to include your details.

Then use the below code to request all “unshipped” and “partially shipped” orders from your preferred $date onwards.

The function invokeListOrders is as below.

That loops through all orders matching the status and date limit previously set. You should store these details in your database so you can check whether you’ve imported the order or not before changing stock (it’s not in the code example above but if you have already imported, don’t call invokeListOrderItems); I don’t know of a way to only import “new” orders, so Amazon will send you everything from the date you specify.

The function invokeListOrderItems gets all items/products from each order, as can be seen here:

As you can see that rather laborious task eventually yields the product information that we need to update the website’s stock – $product_stockcode and $quantity.

You can also access the product name and price amongst numerous other things. Note that the price, which you may or may nor need, is the price of the individual item multiplied by the number of that item purchased, so to get the individual item price we need to divide by the number purchased.

This code is free to use at your own discretion. It comes without warranty. Please feel free to feedback any edits.

Discussion
Add a comment
Saty Jardim

Hello. This is an excellent article. Much more explanatory and simple than the poor and confusing Amazon documentation. Would you be able to tell me if it is possible to update inventory and price in the same XML at the same time?

    Chris Antcliff

    Thanks for the comment Saty. I haven’t had to do that, but I believe you’d need to make a separate call as POST_INVENTORY_AVAILABILITY_DATA just deals with stock/availability.

Rashed

Hello Chris, I have already developed an inventory software using laravel and mysql. I want to integrate amazon with my inventory. Whenever anything will be sold in the Amazon same time my inventory stock will be updated using Amazon API. Is this possible.

    Chris Antcliff

    Hi Rashed,
    Yes, that’s the process the “Updating Website” part of this blog post explains.

LR

It is so much easier to update the Quantity and/or Price using a plain-text feed.

You’ll still use the SubmitFeed API, but set the feed type to “_POST_FLAT_FILE_PRICEANDQUANTITYONLY_UPDATE_DATA_” and set the feed content (in $amazon_feed) to a tab-delimited list having columns sku (required) and quantity and/or price, depending on what you want to update; include the column headers in the first line. To NOT update a field, leave it *empty* (not zero!). (It’s easiest to build/PDO-fetch an array of the lines, then implode(“\n”, $list) the array; just initialize the array with the columns you intend to send.)

This technique avoids limiting the update to 15 items every 20 minutes and a clunky XML feed, but you still have to deal with the per-hour limit on calls to SubmitFeed. Feeds having up to 50,000 rows are processed with minimal or no delay, but if you have more items to update (congratulations! and) you should just queue multiple feeds. YMMV but depending on the size of the file, it takes up to 15 minutes for changes to be sync’d to Seller Central, but the dot-com site updates real-time as the file is processed.

For the second part of the post, where you update your local order data and reduce the local snapshot of available stock: Instead of setCreatedAfter use setLastUpdatedAfter, which will only return orders that have “changed” since the given timestamp; and implement logic to create or update your local record of the order, and decrease stock for the quantity change (so that you don’t over-reduce available stock when multiple units of a single sku get sent in several shipments).

You should also include “Shipped” in the criteria for orderStatuses, to get verification of the final quantity shipped (and to get orders that haven’t been seen in “Unshipped” status); also, for completeness, include the Amazon-misspelled status “Canceled” so you can increase the available stock for units that were ordered but didn’t get shipped.

    Chris Antcliff

    Thanks for the explanation – can’t say I ever came across any of that in Amazon’s documentation!

Rob Bose

Method for updating Amazon stock does not work as there is no parameter for:

AWS secret key.

Response will always be 403.

    Chris Antcliff

    Hi Rob,
    Are you setting $amazonSecretKey in $signature? Probably a bit buried in the code and not explained, sorry.

    I’ve edited the top of the post to include the variables you need to set.

Add a comment