Category and Product SOAP API in Magento

In one of my post I explained the basics of SOAP API, please refer Magento SOAP API before proceeding further

I am listing here the methods to create Categories and Products using SOAP API

1) Category

a) Category Creation

<?php
	$category_name = 'Test Category';			
	
	//create new category
	//start
	$category =  array(
	2,
	array('name'=>$category_name,
	'display_mode' => PRODUCTS_AND_PAGE,
	'is_anchor' => 1,
	'is_active'=>1,
	'default_sort_by'=>'name',
	'available_sort_by'=>'name',
	'custom_design' => 'default/default',
	'custom_design_apply' => 1,
	'page_layout' => 'three_columns'
	)
	//1->yes,2->no //for is_active		
	);		
	
	try {			
		$newCategoryId = $proxy->call(
		$sessionId,
		'category.create',$category
		);			
	}
	catch(Exception $e) 
	{									
		$msg = "Error inserting category: $category_name ".$e->getMessage();
		echo $msg."<br/>";			
	}
		
?>

b) Category deletion

<?php
	// Ensure that category deleted
	try {		
		// Delete product
		$proxy->call($sessionId, 'category.delete', $id);//$id is the category id of category to be deleted		
	} catch (SoapFault $e) {			
	    $msg = "Error deleting category :".$e->getMessage();				    
		echo $msg;
		sendmail($msg,'e');
	}
?>	

2) Product

a) Product Creation

<?php
	$qty = 10;
	$price = 100;
	$stock_status = 1; //in stock
				
	//get attribute list	
	$attributeSets = $proxy->call($sessionId, 'product_attribute_set.list');
	$set = current($attributeSets);
	//for new product
	//start    					
	$newProductData = array(    
	'name'              => 'Test Product',
 	// websites - Array of website ids to which you want to assign a new product
	'websites'          => array(1), // array(1,2,3,...)
	'short_description' => 'Short description goes here',
	'description'       => 'Description goes here',			    	
	'price'             => $price, //set price				
	'status'			=> 1, //for status enable
	//'status'			=> 2, //for status disable
	'tax_class_id'		=> 0,
	'visibility'		=> 4, //for displaying product in Catalog, Search				
	//'visibility'		=> 1, //for not displaying product				
	'manufacturer'		=> 3								
	);   	
	
	
	try {
    	// product creation
		$proxy->call($sessionId, 'product.create', array('simple', $set['set_id'], 'SKU goes here', $newProductData));									
	}
	catch(SoapFault $e) 
	{
		$msg = "Error in inserting product with sku $product_num : ".$e->getMessage();
		echo $msg;		
	}
	
	try {
    	// product updation
		$proxy->call($sessionId, 'product_stock.update', array($product_num, array('qty'=>$qty, 'is_in_stock'=>$stock_status)));
		//1 -> in stock
		//0 -> out of stock 
	}
	catch(Exception $e) {				    
	    $msg = "Error in updating stock information for Product with sku $product_num : ".$e->getMessage();
		echo $msg;		
		
	}				
	//end
?>

b) Product Updation

<?php	
	//for product updation
	//start			
	$updProductData = array(	
	'name'              => 'Test Product',		     	
	'short_description' => 'Short description goes here',
	'description'       => 'Description goes here',				
	//'price'             => '1',	
	'status'			=> 1, //for status enable				
	//'tax_class_id'		=> 0,
	'visibility'		=> 4, //for displaying product in Catalog, Search
	//'manufacturer'		=> 3,					
	//'qty'				=> 1, 
	//'is_in_stock'		=> 1  
	);
	
	try {			    	
		$proxy->call($sessionId, 'product.update', array('SKU goes here',$updProductData));		
	}
	catch(SoapFault $e) 
	{					
		$msg = "Error in updating Product: ".$e->getMessage();
		echo $msg;		
	}				
	//end
?>

b) Product Deletion

<?php
	// Ensure that product deleted
	try {		
		// Delete product
		$proxy->call($sessionId, 'product.delete', $sku); //$sku - SKU of the product		
		//echo "Product with $sku Deleted Successfully<br/>";
	} catch (SoapFault $e) {
	    $msg = "Error deleting product with $sku :".$e->getMessage();
		echo $msg;
		
	}
?>

3) Assigning Product to Category

<?php
try 
{
	$catid = 5; //cateory id to which product to be assigned
	// product assignment
	$proxy->call($sessionId, 'category.assignProduct', array($catid, 'Product SKU goes here'));
	//echo "<br/>Product $product_num assignment successfull";
}
catch(Exception $e) {
	$msg = "Error in assigning product with sku $product_num :".$e->getMessage();
	echo $msg;	
}
?>

Hope this is helpful

8 thoughts on “Category and Product SOAP API in Magento

  1. Note. deleting categories creates orphaned records in the catalog_category_product table, which is used to populate the categories array returned by the catalog_product.info method.

    delete from catalog_category_product where category_id not in (select distinct entity_id from catalog_category_entity);

Leave a Reply

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

Scroll to top