Adding Products to Your eBay Store with the Trading API

In this third and final part of our eBay Trading API series, we’ll be building the product adding functionality into our application.

Creating new Products

Now that we’re done with the store settings, we can begin with writing the code for the controller that would handle the creation of products. Under the controllers directory, create a new file and name it Product.php.

<?php
class Product extends \SlimController\SlimController{
}

Next, create the newAction method. This will render the new.twig file under the templates/product directory. This is the view that renders the form for creating new products.

Breaking it down, at the beginning is where we output the alert message. We’ll pass along the values for this later on when we add the code for creating products. flash is a global variable where session data that we pass along is temporarily saved:

Next is the product title and other required product details. Take note that only the product title, description, quantity, price, and a product image are required in most cases. Here, we’ve added brand because in some cases it’s required. The general term for this kind of field is a conditional field. It usually depends on the category of the product whether it’s required or not. Some categories require more fields than others so be sure to check the documentation on the AddItem API Call to make sure you’re not missing anything.

Breaking the uploadAction() method down, first we specify the upload path. In this case we’re using the uploads directory. Make sure you give it the necessary directory permissions so we can actually upload files to it:

$storage = new \Upload\Storage\FileSystem('uploads');

Next, we specify the field we use for uploading files. Going back to the form for uploading, we didn’t specify any <input type="file"> fields in there. That’s because we don’t need to. By default dropzone uses file as the default field name for uploading files so we specify it as the first argument for the File method, the second argument is the storage which we created earlier:

$file = new \Upload\File('file', $storage);

Next, we give the file a unique name. You can be creative here if you want, but if you’re not feeling creative then just use the uniqid() method to generate a unique file name:

$new_filename = uniqid();
$file->setName($new_filename);

Push the file name into the uploads session item so we can use it later on.

$_SESSION['uploads'][] = $new_filename . '.' . $file->getExtension();

Next, we add some file validation. We just tell it to only accept png, gif, and jpg images, and the max file size should only be 6Mb.

Lastly, we try to upload the file and assign any errors we get and return them to the client side. But we won’t really be making use of this error on the client side since dropzone already has us covered. It displays an x mark for the uploaded file if the file didn’t pass the validation:

That allows us to specify the options for the dropzone. Here’s a breakdown of each option:

acceptedFiles – the type of file that can be uploaded.

autoProcessQueue – a boolean value for specifying whether to process the queue immediately or not.

uploadMultiple – a boolean value for specifying whether to allow multiple file uploads. In this case we selected false but it doesn’t mean we can’t upload more than one image. It means that we can’t upload more than one image at the same time.

maxFiles – an integer value for specifying the max number of files which can be uploaded.

What the code above does is make a POST request every time the user tabs out of the product title field. The request then returns an array of objects containing the suggested categories based on the product title. The method which is used in this call is in the Product class. What it does is call the getSuggestedCategories() method in the Ebay class, passing in the product title as the argument. The response is then converted into a JSON string and echoed out:

What the method above does is make a request to the GetSuggestedCategories API method. This call accepts the product title as its argument, as passed in the Query field. If the call is successful, it returns an array of suggested categories. We just need to extract the category id and category name from the result it returns.

Now we’re ready to write the createAction method. This is where we save the product details into the database and create the product on ebay.

Breaking it down. First, we declare a new instance of valitron, passing in all the values that are currently stored in the $_POST global variable:

$v = new Valitron\Validator($_POST);

Next we add the rules. Basically all the fields are required, the price should be a numeric value, and the quantity should be an integer. We specified that using the rule method which takes up the actual rule as the first argument, and the name of the field as the second argument:

Next we validate using the validate() method. If this returns true then the validation passed, otherwise it didn’t. If it returns false then we take all the POST data and save it into the session along with the error messages returned by valitron.

If the validation passes, we retrieve the store settings from the database and supply them as an argument for the addItem() method along with the contents of the $_POST variable. The addItem() method creates the product on eBay. If the call is successful, we create a prepared statement for adding the details of the product in the database. Once that’s done we flash a message into the session saying that the product was created. If the call is not successful then we pass the response returned from the API along with a message that the product was not created.

Next, we’ll define the addItem method in the Ebay class. Define 2 parameters: $store_settings and $item_data. Inside the method declare a variable called $boolean_value. This allows us to convert the integer value 0 to a string false and the integer value 1 to a string true. We need this because the API only accepts either true or false for boolean parameters.

Next, assign the uploads session item to a variable. If you remember from earlier, this session item is where we pushed the filenames of the images that the user uploaded. Next is the $baseupload_url, this contains the base URL in which all uploaded images can be found. We need this because the UploadSiteHostedPictures API method only accepts a URL for uploading to ebay. This also means that you need to have an actual server that’s accessible via public internet. The UploadSiteHostedPictures method only accepts a single URL at a time so we have to loop through all the images and supply the actual URL as the value for the ExternalPictureURL field. If the request is successful we just append the new image URL that’s returned to the $picture_details variable. This will be used as one of the arguments passed to the call for actually creating the product on eBay.

Next, we make the request for adding the product to eBay. This can be done via either the AddItem method or the VerifyAddItem method. The AddItem method is the one that you would want in production, since it adds the product to ebay. If there are any fees, those are also applied. VerifyAddItem on the other hand acts like a sandbox method, it doesn’t actually add the item on ebay. You can use it if you want to test the AddItem call. No fees are applied when you call it, but it lets you know how much of a fee will be applied or if the request would actually be successful. Note that you don’t need to use the sandbox version of the API in order to call the VerifyAddItem method. You can use it even on the live version of the API.

To make things clearer, here’s a breakdown of the parameters we passed to the AddItem request. Here we’re passing in the seller profiles. The only requirement for each seller profile is the profile ID. We already got those earlier when we fetched the store settings, so we simply get them from the database.

OutOfStockControl is a boolean value for specifying whether to keep the listing alive after it reaches a quantity of 0. Setting this to true excludes the listing from search results and from being viewed.

Conclusion

That’s it! In this tutorial we went through eBay’s Trading API. We have learned that we can talk to their API by acquiring a token for the user. We also walk through fetching the user settings, uploading images and adding products using the API. But that’s not all you can do with Ebay’s Trading API. It’s a pretty huge API and almost anything that you can think of that you can do with eBay with regards to selling products, you can also do with the API. So be sure to check the docs if you’re curious. You can check out the source code of the project we’ve built in this github repository.

Wern is a web developer from the Philippines. He loves building things for the web and sharing the things he has learned by writing in his blog. When he's not coding or learning something new, he enjoys watching anime and playing video games.