Magento – how to Import category, attribute and product through API call

apiattributescategoryimportmagento-1.8

I'm a web developer of a company that produce ERP software.
I would like to integrate the ERP software with Magento ecommerce solution with automatic synchronization function through API call.
I've read a lot of information about SOAP Api call but i didn't understand if that will be possible.

I read something about "createacategory" SOAP method, but nothing about attributes.

I need to create a function that can be usefull for every single client-company.

That's why i need to put all the necessary information about product through auto import method.

How can i do that? it's possible? there is a method for importing attributes?

Best Answer

As i understand your query, here is the method to get the api data, into xml format first.

$xml contain xml response you have made to get the data from api.

$soap_do = curl_init();
            curl_setopt($soap_do, CURLOPT_URL,            $url );
            curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
            curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($soap_do, CURLOPT_POST,           true );
            curl_setopt($soap_do, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($soap_do, CURLOPT_POSTFIELDS,    $xml);

            $result = curl_exec($soap_do);

            curl_close($soap_do);
            //echo $result;
            //echo $result_array = json_encode($result);
            //echo   $description = $result->extendeddescription;

$p = xml_parser_create();
xml_parse_into_struct($p, $result, $vals, $index);
xml_parser_free($p);

After you can break the xml into array and get the value. here $vals contains the valid array of xml comes data. If you want to import the product as find into xml array you need to run new product making script with valid input param comes form api.

The Below code, you can used to add or update new category

$category = Mage::getModel('catalog/category');
                                                  $category->setStoreId(0); 
                                                  $categoryName = $cate;
                                                  $rootCategory['name'] = $categoryName;
                                                  $rootCategory['path'] = "1/2"; 
                                                  //$rootCategory['display_mode'] = "PRODUCTS";
                                                  $rootCategory['is_active'] = 1;

                                                  $category->addData($rootCategory);
                                                  try 
                                                  {
                                                    $category->save();
                                                    $CategoryId = $category->getId();
                                                  }
                                                  catch (Exception $e)
                                                  {
                                                    echo "categories not found";
                                                  }  

And for product adding you need to follow the below script with valid input params

$obj = Mage::getModel('catalog/product');
$_product = $obj->load($productid); // Enter your Product Id in $product_id
// get Product's name
$name = $_product->getName();
//get product's short description
$shortdes = $_product->getShortDescription();
//get Product's Long Description
$des = $_product->getDescription();
//get Product's Regular Price
//echo $_product->getPrice();
//get Product's Special price
//echo $_product->getSpecialPrice();
//get Product's Url
//echo $_product->getProductUrl();
//get Product's image Url
//echo $_product->getImageUrl();
//echo "<pre>";print_r($_GET);exit;

/*********Getting values*********/

$optionval1 = $_GET['optionval1'];
$optionval2 = $_GET['optionval2'];
$optionval3 = $_GET['optionval3'];
$optionval4 = $_GET['optionval4'];
$image = $_GET['image'];
//print_r($image);
$image_parts = explode('/',$image);
$count = count($image_parts);
$final_image = $image_parts[$count-1];
//echo "<pre>";print_r($image_parts[$count-1]);exit;
$price = $_GET['price'];
$product = Mage::getModel('catalog/product');
$product_sku = time();
$product->setSku($product_sku);
$product->setName($name);
$product->setDescription($des);
$product->setShortDescription($shortdes);
//-- price section
$product->setPrice($price);
//$product->setSpecialPrice(10);

//----- company address and map
$product->setTypeId('simple');
$product->setAttributeSetId(9); // default
$product->setCategoryIds(array(20)); // need to look these up

$product->setWeight(1.0);
$product->setTaxClassId(2); // taxable goods
$product->setVisibility(3); // search
$product->setStatus(1); // enabled
$productQty = '1';

//$stockData=$product->getStockData();
$stockData['qty']=$productQty;
$stockData['is_in_stock']=1;
$stockData['manage_stock']=1;
$stockData['use_config_manage_stock']=0;
$product->setStockData($stockData);

// adding images to product
$abspath = getcwd();

$path = "./final_images/new/".$final_image;
//print_r($path);
?>

<?php 
if (file_exists($path)) {
    $product->addImageToMediaGallery($path,array('image','small_image','thumbnail'),false,false);
}

$product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
$product->save();
/*********** custom option for cart ***********/
  $new_product_id=$product->getId();
                                        $product = Mage::getModel('catalog/product')->load($new_product_id);
                                        $optionData = array(
                                                'is_require'        => false,
                                                'title'             => 'Select Font',
                                                'type'              => 'drop_down',
                                                'sort_order'        => 0,
                                                'values'            => array(
                                                    array(
                                                        'is_delete'     => 0,
                                                        'title'         => '12',
                                                        'price_type'    => 'fixed',
                                                        'price'         => '',
                                                        'sku'           => '',
                                                        'sort_order'        => 0,
                                                    ),
                                                    array(
                                                        'title'         => '18',
                                                        'price_type'    => 'fixed',
                                                        'price'         => '',
                                                        'sku'           => '',
                                                        'sort_order'        => 1,
                                                )),
                                            );
                                        $product->setHasOptions(1);
                                        $option = Mage::getModel('catalog/product_option')
                                                  ->setProductId($new_product_id)
                                                  ->setStoreId(1)
                                                  ->addData($optionData);
                                        $option->save();
                                        $product->addOption($option);
                                        Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
                                        $product->save();
/*********** custom option for cart end***********/
$productid = $product->getId();
$result['product'] = $productid;
echo $result['product'];

Hope this will help you, or make sense what you should need to do.