Search code examples
phpmagentomagento-soap-api

Magento Product Create via API not working


I have been fighting with the Magento SOAP web service for a week and I can not figure out why I can not create a product using the API.

Below is my PHP code:

$client = new SoapClient('http://mywebsie.com/wp/store/api/soap/?wsdl');

// If some stuff requires api authentification,
// then get a session token
$session = $client->login('apiuser', 'apikey');

// get attribute set
$attributeSets = $client->call($session, 'product_attribute_set.list');
$attributeSet = current($attributeSets);

    $newProductData = array(    
    'name'              => 'Test product',
    'websites'          => array(1), 
    'short_description' => 'This is the short desc',
    'description'       => 'This is the long desc',                  
    'price'             => 150.00,                
    'status'            => 1,
    'tax_class_id'      => 0,
    'visibility'        => 4                             
    );      


    try {
        // product creation
        $client->call($session, 'product.create', array('simple', $set['set_id'], $ItemNmbr, $newProductData));                                   
    }
    catch(SoapFault $e) 
    {
        $msg = "Error in inserting product with sku $ItemNmbr : ".$e->getMessage();
        echo $msg;      
    }

I am getting the following error:

Error in inserting product with sku ING-ACCS-00009 : Invalid data given. Details in error message.

Solution

  • I think you got this code from http://www.magentocommerce.com/api/soap/catalog/catalogProduct/catalogProduct.html. There are some bugs. Here is a fixed version:

    $client = new SoapClient('http://mywebsie.com/wp/store/api/soap/?wsdl');
    
    // If some stuff requires api authentification,
    // then get a session token
    $session = $client->login('apiuser', 'apikey');
    
    $newProductData = array(
        'name' => 'Test product',
        'websites' => array(1),
        'short_description' => 'This is the short desc',
        'description' => 'This is the long desc',
        'price' => 150.00,
        'status' => 1,
        'tax_class_id' => 0,
        'url_key' => 'product-url-key',
        'url_path' => 'product-url-path',
        'visibility' => '4',
    );
    
    $sku = 'Some unique sku';
    $storeView = 1;
    $attributeSetId = 4; // you can get this id from admin area
    $productType = 'simple';
    
    try {
        // product creation
        $client->call($session, 'catalog_product.create', array($productType, $attributeSetId, $sku, $newProductData, $storeView));
    } catch (SoapFault $e) {
        echo "Error in inserting product with sku $sku : " . $e->getMessage();
    }