A client of ours sells products on eBay as well as their own website and they needed to be able to ensure stock levels were correct on both platforms. This required the use of eBay’s API to manipulate stock on eBay when there was a sale on the website and vice versa. Let’s take a look at how we acheived this.

Updating eBay

This is the XML you need to send eBay to change the stock level of an item, assigned to a variable called $post which we’ll use later.

Pretty simple; you include the Auth Token that identifies your eBay account, the relevant Item (product) ID and the total Quantity you want to declare as in stock.

To send this we use cURL:

The above code sets the headers which include more identificatory information (which you will get from your eBay developer account) plus the Call Name. In this case we are wanting to change stock, so need to use ReviseInventoryStatus (http://developer.ebay.com/devzone/xml/docs/reference/ebay/ReviseInventoryStatus.html).

We then specify the eBay URL to which this information will be sent, along with the XML we created earlier.

The result of this call is stored in $response as XML. It’s a good idea to store it, plus $post,  in some form so you can track what happened.

Updating Website

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

We need to import orders. This is the XML we need to send:

Again we use our Auth Token and this time we specify we want completed orders (OrderStatus) from the past day (NumberOfDays) where we are the seller (OrderRole).

This is the cURL we use to POST to eBay:

As you can see it’s very similar to the first code, but the Call Name changes to GetOrders (http://developer.ebay.com/devzone/xml/docs/reference/ebay/GetOrderTransactions.html).

The return is XML which will contain any orders which match the criteria. From here you access the data as such:

As you can see there’s a lot of data that can be accessed but I’ve tried to make the variables self-explanatory, with comments where I deemed necessary.

Now you can update the website stock based on $stockcode and $quantity or even import the new order and/or customer to your own database. Note: $quantity here is how many were purchased, not how many are in stock.

You’ll need to check whether you’ve already imported the order or not before changing stock but apart from that, this will just about get you there.

 

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

Add a comment