« Introducing Printfection Quick Merch | Main | Developer Ideas Notebook - Dynamic Product Creation »
Thursday
Dec042008

Building Your First Application: Part 6 - Creating Products

The final step towards actually having a product to sell, is to use the Printfection API to create a product.

Creating a basic product is simple using the Printfection.Products.create method. This method has only four required parameters that must be sent in the request:

  • rootid - The root product for your new item
  • sectionid - where you want the product created
  • rootcolorids - a list of colors that you want available
  • specs - an array of design options to use to create your product

There are however some important concepts to understand to make the process smooth.

Root Products

Every product created at Printfection is based on a root product. A root product is the blank item that you want to base your custom product on. You can see the current range of root products in our catalog.

Each item has a unique ID to identify it to the API. Take the following root products as an example:

T-Shirt - Rootid 7Mens Ringer T - Rootid 20Jumbo Tote Bag - Rootid 18

 

 

You can retrieve a full list of root products using the following PFQL:

SELECT rootid, title FROM root_products WHERE 1 ORDER BY title

Root Colors

Each root product has its own unique set of root colors that are used to add color selections to your root products. You can send a single color, or a comma separated list with your request.

You can retrieve a full list of root products using the following PFQL ($rootID must be a valid root product):

SELECT rootcolorid FROM root_product_colors WHERE rootid= $rootID

Product Specs

The final parameter that we need to use when creating a basic product, is the specs. The specs tell the api how you actually want your design set upon the root product. The specs are sent as a serialized JSON array and contain the following items:

  • side - 0 (front) or 1 (back)
  • imageid - the image that you want on your product
  • position - (optional) The position on the root product to use.
  • center_percent_x - (optional) The percent from the top of the position to the center of the image.
  • center_percent_y - (optional) The percent from the left of the position to the center of the image.
  • rotation - (optional) Possible Values: 0,90,180,270 The rotation of the image.
  • height - The height in inches of the image

In PHP, a sample specs variable would like like this:

$specs = '
{
"SPECS": {
"side": 0,
"imageid": "'.$imageID.'",
}
}';

Putting it all together

So now, to create our product, it is simply a matter of sending our request with all the parameters attached. As a sample, the following PHP code will use cURL to send the request:

$api_sig_tmp2 = "api_key=".$api_key."method=".$method."rootcolorids=".$rcid."rootid=".$rootid."sectionid=".$sectionid."session_key=".$sessionID."specs=".$specs."version=1.0".$secret;
$api_sig2 = md5($api_sig_tmp2);

$post_data = array();
$post_data['api_key'] = $api_key;
$post_data['api_sig'] = $api_sig2;
$post_data['method'] = $method;
$post_data['rootcolorids'] = $rcid;
$post_data['rootid'] = $rootid;
$post_data['sectionid'] = $sectionid;
$post_data['session_key'] = $sessionID;
$post_data['specs'] = $specs;
$post_data['version'] = "1.0";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://api.printfection.com/restserver.php" );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$postResult = curl_exec($ch);
if (curl_errno($ch)) {
print curl_error($ch);
}
curl_close($ch);

That will return an XML document that contains the productid for the product that you have just created.  You should store this in a variable for use in the next part, where we add some more information, as well as set our commision for the product.

 

 

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
All HTML will be escaped. Textile formatting is allowed.