Magento: Adding/Updating Products with V2 API

Magento unfortunately has virtually no documentation on the V2 API. I needed to prove to someone today that using the V2 API, you can indeed create a product, assign it to 2 or more websites, and set the product price to different values for each website (as long as you’ve set the price scope to “Website” instead of the default “Global” which is in System > Configuration > Catalog > Price).

In this example, I set the initial product data to create, and run the API method ‘catalogProductCreate’. After the product has been created, I then set the product price for the wholesale website, and then run the ‘catalogProductUpdate’ method. The key here is passing in the correct store view code. Ultimately, the product is created with the price of $45. Then the wholesale website is updated to set a price of $20.

<?php
 
//error_reporting(E_ALL);
//ini_set('display_errors', '1');
 
/**
 * Get API Session
 */
$client = new SoapClient('http://core.local/api/v2_soap?wsdl=1');
$session = $client->login('augustash', 'password');
 
/**
 * Set Product Data
 */
$newProductData                     = new stdClass();
$newProductData->name               = 'Product Name';
$newProductData->description        = 'Description';
$newProductData->short_description  = 'Short Description';
$newProductData->websites	    = array(1,2);
$newProductData->categories         = array(7,15);
$newProductData->status             = 1;
$newProductData->price              = 45;
$newProductData->tax_class_id       = 2;
$newProductData->weight             = 1;
 
/**
 * Create Product Using V2 API
 */
$result = $client->catalogProductCreate(
    $session,           // Soap Session
    'simple',           // Product Type
    4,                  // Attribute Set Id (Default)
    'product-sku',      // Product Sku
    $newProductData     // Product Data
);
 
 
/**
 * Set Price for Wholesale Website
 */
$newProductData         = new stdClass();
$newProductData->price  = 20;
 
/**
 * Update Product with Wholesale Price using V2 API
 */
$result = $client->catalogProductUpdate(
    $session,           // Soap Session
    'product-sku',      // Product Sku
    $newProductData,    // Product Data
    'wholesale'         // Store View Code
);
 
 
/**
 * End Session
 */
$client->endSession($session);

If you are wondering where to look to find out what values you can pass in, and what parameters are needed when making V2 API calls, you’ll need to look at the wsdl. In app/code/core/Mage/Catalog/etc/wsdl.xml, you’ll find the product API info. I’ve extracted the product data fields that you can pass in when creating a product:

<complexType name="catalogProductCreateEntity">
    <all>
        <element name="categories" type="typens:ArrayOfString" minOccurs="0" />
        <element name="websites" type="typens:ArrayOfString" minOccurs="0" />
        <element name="name" type="xsd:string" minOccurs="0" />
        <element name="description" type="xsd:string" minOccurs="0" />
        <element name="short_description" type="xsd:string" minOccurs="0" />
        <element name="weight" type="xsd:string" minOccurs="0" />
        <element name="status" type="xsd:string" minOccurs="0" />
        <element name="url_key" type="xsd:string" minOccurs="0" />
        <element name="url_path" type="xsd:string" minOccurs="0" />
        <element name="visibility" type="xsd:string" minOccurs="0" />
        <element name="category_ids" type="typens:ArrayOfString" minOccurs="0" />
        <element name="website_ids" type="typens:ArrayOfString" minOccurs="0" />
        <element name="has_options" type="xsd:string" minOccurs="0" />
        <element name="gift_message_available" type="xsd:string" minOccurs="0" />
        <element name="price" type="xsd:string" minOccurs="0" />
        <element name="special_price" type="xsd:string" minOccurs="0" />
        <element name="special_from_date" type="xsd:string" minOccurs="0" />
        <element name="special_to_date" type="xsd:string" minOccurs="0" />
        <element name="tax_class_id" type="xsd:string" minOccurs="0" />
        <element name="tier_price" type="typens:catalogProductTierPriceEntityArray" minOccurs="0" />
        <element name="meta_title" type="xsd:string" minOccurs="0" />
        <element name="meta_keyword" type="xsd:string" minOccurs="0" />
        <element name="meta_description" type="xsd:string" minOccurs="0" />
        <element name="custom_design" type="xsd:string" minOccurs="0" />
        <element name="custom_layout_update" type="xsd:string" minOccurs="0" />
        <element name="options_container" type="xsd:string" minOccurs="0" />
        <element name="additional_attributes" type="typens:associativeArray" minOccurs="0" />
    </all>
</complexType>

Here are the parameters that can be passed in when using the create and update methods:

<message name="catalogProductCreateRequest">
    <part name="sessionId" type="xsd:string" />
    <part name="type" type="xsd:string" />
    <part name="set" type="xsd:string" />
    <part name="sku" type="xsd:string" />
    <part name="productData" type="typens:catalogProductCreateEntity" />
</message>
 
<message name="catalogProductUpdateRequest">
    <part name="sessionId" type="xsd:string" />
    <part name="product" type="xsd:string" />
    <part name="productData" type="typens:catalogProductCreateEntity" />
    <part name="storeView" type="xsd:string" />
    <part name="productIdentifierType" type="xsd:string" />
</message>
This entry was posted in Magento. Bookmark the permalink.

3 Responses to Magento: Adding/Updating Products with V2 API

  1. Dylan says:

    This is great documentation! thanks so much.

  2. Miley says:

    This is a really helpful post. It should be up on the Magento Wiki. I have been at this for hours wondering what should be passed in.

    Much appreciated
    Miley
    “I want to die quietly in my sleep, like my grand father… not running around screaming like his passengers”

  3. Andrew says:

    I can’t get this code example to work. I’ve just installed a brand new Magento 1.7, created the api user role, and ran this script. I get: Error cannot find parameter. Can anyone point me in the right direction?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>