.net C# API to Magento via XML-RPC

Since I’m starting to get back to writing .Net C# again, I decided to dig around for anything Magento related and found a nice XML-RPC based library, that will allow you to utilize Magento’s API. Samples are provided and seem to be pretty straight forward, however they do note a lot of the returns even though stated as an integer or such, but tend to most always return a string. Which leads me to believe its something with the XML-RPC in between the two, and PHP’s love to turn most any type into a string. Here is one of the examples to get your gears turning:

#region Product Image Examples
//// gets the product image current store view (doc says int returned, but is string)
string myProductImageCurrentStore = ProductImage.CurrentStore(apiUrl, sessionId, new object[] { });
Console.WriteLine(myProductImageCurrentStore);

You can find the full API here, along with some notes:
arrow .NET C# Object Library for Magento

arrow .net C# API to Magento via XML-RPC

Direct download link to the API:
arrow Ez.Newsletter.MagentoApi_v1.1.zip

Exporting users/customers from X-Cart to Magento

title-images-import2

Following my previous posting of importing basic product data into Magento from X-Cart I stumbled across this code on the Magento Community forums with a nifty PHP script to prepare a CSV file of users to be imported into Magento.

:!: UPDATE: Finally got around to trying to utilize this script, and realized there are in fact some issues with using it. Some of the main items I noticed is:

  • It is not 3.5.x X-Cart compatible, and possibly lower versions.
  • As well as it was not very optimized for a very large number of customers, causing memory limit errors while attempting to export.

I have updated the code to handle the two items mentioned above and have successfully imported almost 6k customers from a 3.5.x version of X-Cart. Here is the updated script, the same instructions apply, copy the file into your admin directory of X-Cart and then proceed to login to the admin of X-Cart, after successfully logging in, simply change index.php in your url to migrate.php and wait for the script to prompt for you to download a .csv file of the users. Be patient especially if you have a large number of customers as the script can take awhile to generate this data without any prompts that it is working.

idea When using this code be sure and note the following comments as, depending on your X-Cart version. Anonymous customers are ignored.

Create the file $xcart_dir/admin/migrate.php and copy the following code into it:

<?php
/*
Migrate X-Cart customers to Magento
Original by Spydor at: http://www.magentocommerce.com/boards/viewthread/30894/
Modified by B00MER at: http://www.molotovbliss.com 3/15/2009 3:49:23 AM
Modifications include:
X-Cart 3.5 compatibility, possibly lower versions as well.
Optimization for large number of records to avoid memory limit errors
*/
@set_time_limit(2700);
# Include core functions
if (!require("../admin/auth.php"))
	require("./auth.php");
function resolveState($b_state,$b_country,$s_state,$s_country){
	global $states;
	$result = array();
	foreach($states as $key=>$value){
		// Billing
		if(($value['state_code']==$b_state) && ($value['country_code']==$b_country))
			 $result['billing'] = $value['state'];
		// Shipping
		if(($value['state_code']==$s_state) && ($value['country_code']==$s_country))
			 $result['shipping'] = $value['state'];
	}
	if(empty($result['billing']))
		$result['billing'] = $b_state;
	if(empty($result['shipping']))
		$result['shipping'] = $s_state;
	return $result;
}
function func_export_csv($data,$title) {
	$output = $data;
	$size_in_bytes = strlen($output);
	   header("Content-type: application/vnd.ms-excel");
	   header("Content-disposition: csv; filename=".$title . '_' . date("Y-m-d") . ".csv; size=$size_in_bytes");
	   return $output;
}
# Define new line
$newline = "\n";
# Define CSV fields
$output =  '"website","email","group_id","prefix","firstname","middlename","lastname","suffix","password_hash","taxvat","billing_prefix","billing_firstname","billing_middlename","billing_lastname","billing_suffix","billing_street_full","billing_city","billing_region","billing_country","billing_postcode","billing_telephone","billing_company","billing_fax","shipping_prefix","shipping_firstname","shipping_middlename","shipping_lastname","shipping_suffix","shipping_street_full","shipping_city","shipping_region","shipping_country","shipping_postcode","shipping_telephone","shipping_company","shipping_fax","created_in","is_subscribed"';
$output .= "$newline";
$states = func_query("SELECT $sql_tbl[states] .state, $sql_tbl[states] .code AS state_code, $sql_tbl[states] .country_code FROM $sql_tbl[states], $sql_tbl[countries] WHERE $sql_tbl[states] .country_code=$sql_tbl[countries] .code AND $sql_tbl[countries] .active='Y'");
# total customers
$sql = "SELECT COUNT(*) AS total_results FROM $sql_tbl[customers] where login NOT LIKE 'anonymous%'";
$results = func_query($sql);
$total_results=$results[0]["total_results"];
# number of records to return each loop
$num_of_records=150;
# start for loop to feed out data
for($i=0; $i<=$total_results; $i+=$num_of_records) {
  #$q = "SELECT * FROM xcart_customers where login NOT LIKE 'anonymous%' "; // Memory Hog with SELECT *
  $q = "SELECT email,title,firstname,lastname,password,title,b_address,b_city,b_country,b_zipcode,phone,company,fax,s_address,s_city,s_country,s_zipcode FROM xcart_customers where login NOT LIKE 'anonymous%' LIMIT $i,$num_of_records ";
  $result = func_query($q);
  foreach($result as $key=>$value){
       # Uncomment for version 4.0+ of X-Cart
  	   $result[$key]['password_hash'] = md5(text_decrypt($value['password']));
       # for version 3.5 and below of X-Cart
  	   #$result[$key]['password_hash'] = text_decrypt($value['password']);
  	   $realstates = resolveState($value['b_state'], $value['b_country'], $value['s_state'], $value['s_country']);
  	   $result[$key]['b_real_state'] = $realstates['billing'];
  	   $result[$key]['s_real_state'] = $realstates['shipping'];
  }
  foreach($result as $key => $value) {
    #func_print_r($result);exit;
  	$output .= '"base",';
  	$output .= '"' . $value['email'] . '",';
  	$output .= '"General",';
  	$output .= '"' . $value['title'] . '",';
  	$output .= '"' . $value['firstname'] . '",';
  	$output .= '"",';
  	$output .= '"' . $value['lastname'] . '",';
  	$output .= '"",';
  	$output .= '"' . $value['password_hash'] . '",';
  	$output .= '"",';
  	# Billing info
  	$output .= '"' . $value['title'] . '",';
  	$output .= '"' . $value['firstname'] . '",';
  	$output .= '"",';
  	$output .= '"' . $value['lastname'] . '",';
  	$output .= '"",';
  	$output .= '"' . $value['b_address'] . '",';
  	$output .= '"' . $value['b_city'] . '",';
  	$output .= '"' . $value['b_real_state'] . '",';
  	$output .= '"' . $value['b_country'] . '",';
  	$output .= '"' . $value['b_zipcode'] . '",';
  	$output .= '"' . $value['phone'] . '",';
  	$output .= '"' . $value['company'] . '",';
  	$output .= '"' . $value['fax'] . '",';
  	# Shipping Info
  	$output .= '"' . $value['title'] . '",';
  	$output .= '"' . $value['firstname'] . '",';
  	$output .= '"",';
  	$output .= '"' . $value['lastname'] . '",';
  	$output .= '"",';
  	$output .= '"' . $value['s_address'] . '",';
  	$output .= '"' . $value['s_city'] . '",';
  	$output .= '"' . $value['s_real_state'] . '",';
  	$output .= '"' . $value['s_country'] . '",';
  	$output .= '"' . $value['s_zipcode'] . '",';
  	$output .= '"' . $value['phone'] . '",';
  	$output .= '"' . $value['company'] . '",';
  	$output .= '"' . $value['fax'] . '",';
  	$output .= '"default",';
  	$output .= '"0"';
  	$output .= "$newline";
  }
}
#func_print_r($output); // uncomment to see output
print func_export_csv($output,"xcart_customers");
?>

Here is the original code by Spydor:

<?php
require "../auth.php";
$states = func_query("SELECT $sql_tbl[states] .state, $sql_tbl[states] .code AS state_code, $sql_tbl[states] .country_code FROM $sql_tbl[states], $sql_tbl[countries] WHERE $sql_tbl[states] .country_code=$sql_tbl[countries] .code AND $sql_tbl[countries] .active='Y'");
#print_r($states);
$q = "SELECT * FROM xcart_customers where login NOT LIKE 'anonymous%' ";
$result = func_query($q);
foreach($result as $key=>$value){
       $result[$key]['password_hash'] = md5(text_decrypt($value['password']));
       $realstates = resolveState($value['b_state'], $value['b_country'], $value['s_state'], $value['s_country']);
       $result[$key]['b_real_state'] = $realstates['billing'];
       $result[$key]['s_real_state'] = $realstates['shipping'];
}
#print_r($result);
$output =  '"website","email","group_id","prefix","firstname","middlename","lastname","suffix","password_hash","taxvat","billing_prefix","billing_firstname","billing_middlename","billing_lastname","billing_suffix","billing_street_full","billing_city","billing_region","billing_country","billing_postcode","billing_telephone","billing_company","billing_fax","shipping_prefix","shipping_firstname","shipping_middlename","shipping_lastname","shipping_suffix","shipping_street_full","shipping_city","shipping_region","shipping_country","shipping_postcode","shipping_telephone","shipping_company","shipping_fax","created_in","is_subscribed"';
$output .= "1512";
foreach($result as $key => $value){
    $output .= '"base",';
    $output .= '"' . $value['email'] . '",';
    $output .= '"General",';
    $output .= '"' . $value['title'] . '",';
    $output .= '"' . $value['firstname'] . '",';
    $output .= '"",';
    $output .= '"' . $value['lastname'] . '",';
    $output .= '"",';
    $output .= '"' . $value['password_hash'] . '",';
    $output .= '"",';
    # Billing info
    $output .= '"' . $value['title'] . '",';
    $output .= '"' . $value['firstname'] . '",';
    $output .= '"",';
    $output .= '"' . $value['lastname'] . '",';
    $output .= '"",';
    $output .= '"' . $value['b_address'] . '",';
    $output .= '"' . $value['b_city'] . '",';
    $output .= '"' . $value['b_real_state'] . '",';
    $output .= '"' . $value['b_country'] . '",';
    $output .= '"' . $value['b_zipcode'] . '",';
    $output .= '"' . $value['phone'] . '",';
    $output .= '"' . $value['company'] . '",';
    $output .= '"' . $value['fax'] . '",';
    # Shipping Info
    $output .= '"' . $value['title'] . '",';
    $output .= '"' . $value['firstname'] . '",';
    $output .= '"",';
    $output .= '"' . $value['lastname'] . '",';
    $output .= '"",';
    $output .= '"' . $value['s_address'] . '",';
    $output .= '"' . $value['s_city'] . '",';
    $output .= '"' . $value['s_real_state'] . '",';
    $output .= '"' . $value['s_country'] . '",';
    $output .= '"' . $value['s_zipcode'] . '",';
    $output .= '"' . $value['phone'] . '",';
    $output .= '"' . $value['company'] . '",';
    $output .= '"' . $value['fax'] . '",';
    $output .= '"default",';
    $output .= '"0"';
    $output .= "1512";
}
print func_export_csv($output,"xcart_customers") ;
function resolveState($b_state,$b_country,$s_state,$s_country){
    global $states;
    $result = array();
    foreach($states as $key=>$value){
        // Billing
        if(($value['state_code']==$b_state) && ($value['country_code']==$b_country))
             $result['billing'] = $value['state'];
        // Shipping
        if(($value['state_code']==$s_state) && ($value['country_code']==$s_country))
             $result['shipping'] = $value['state'];
    }
    if(empty($result['billing']))
        $result['billing'] = $b_state;
    if(empty($result['shipping']))
        $result['shipping'] = $s_state;
    return $result;
}
function func_export_csv($data,$title) {
    $output = $data;
    $size_in_bytes = strlen($output);
       header("Content-type: application/vnd.ms-excel");
       header("Content-disposition: csv; filename=".$title . '_' . date("Y-m-d") . ".csv; size=$size_in_bytes");
       return $output;
}

Here is the original posting by Spydor:
Magento – Migrating users from Xcart to Magento – General Forum – eCommerce Software for Growth.

Export X-Cart Products for Magento

xcart-to-mag

Something I have been wanting to do for awhile is attempt at writing a Magento X-Cart import module. Since I’m no where near getting such completed, and was needing to get a clients old X-Cart version 3.5.X product data imported into Magento 1.2.x, I decided to do it the old fashioned way. Export the X-Cart data into an Excel document and map the exported data. Since I had no idea what fields Magento would require for the import. I simply exported my existing Magento products I had added manually. If you don’t have any simply create a single test product and then proceed to export from Magento.

Please keep in mind this is a very basic import, as Product Options and Categories from X-Cart are ignored. I’m open to any ideas and/or thoughts to achieve this, so please feel free to add any comments.

Here is the SQL Query that I have used to grab the basic essential data out of X-Cart:

SELECT
xcart_products.productid,
xcart_products.productcode,
xcart_products.product,
xcart_products.brand,
xcart_products.model,
xcart_products.weight,
xcart_products.categoryid,
xcart_products.list_price,
xcart_products.descr,
xcart_products.fulldescr,
xcart_products.param00,
xcart_products.param01,
xcart_products.param02,
xcart_products.param03,
xcart_products.`type`,
xcart_pricing.productid,
xcart_pricing.quantity,
xcart_pricing.price,
xcart_thumbnails.image_path,
xcart_images.image_path,
xcart_images.image_x,
xcart_images.image_y,
xcart_images.image_size,
xcart_images.alt,
xcart_pricing.membership,
xcart_categories.category
FROM
xcart_products
Left Join xcart_pricing ON xcart_products.productid = xcart_pricing.productid
Left Join xcart_thumbnails ON xcart_products.productid = xcart_thumbnails.productid
Left Join xcart_images ON xcart_products.productid = xcart_images.productid
Left Join xcart_categories ON xcart_categories.categoryid = xcart_products.categoryid
GROUP BY xcart_products.productid

xcart-import-diagram-schema   

One Remote MySQL Manager I found very useful for alot of this was “NaviCat“, the free version doesn’t support export/importing however they do offer a 30 day trial with all the bells and whistles.

As you can tell I am only exporting xcart_products, xcart_pricing, xcart_thumbnails, xcart_images and xcart_categories. However I did not use xcart_categories when mapping my data in Excel, I merely wanted to export it so it was easier to determine products while in Excel.

When exporting with NaviCat I noticed two Excel options, both seem to yield the same results, however I noticed with one of the options long descriptions over 255 characters were being truncated, here is the option to use for best results I’ve found:
export option

A few things to take note while mapping and/or copying your X-Cart exported data to your Magento exported data is:

  • You will need to remove the full path that X-Cart uses in its image tables, i.e. /home/mysite/public_html/
  • Keep a front slash ‘/’ in front of your image filename’s.
  • Copy all of your X-Cart product’s Images via FTP or however you choose. And upload them to /media/import/
  • Keep existing data in the fields of the Magento products export the same. The most note-able are: store, attribute_set and type.
  • You may have some custom data from X-Cart that can be utilized, per say if you were using param00 for a cost field, you can easily map this field into Magento’s custom attributes you can create to be able to port this information over. You can find custom attributes under “Catalog”->”Attributes”->”Manage Attributes” within the Admin.
  • Export as a “Comma Separated Values” (CSV) file. I opened this file up in notepad after exporting and re-saved just to ensure line-breaks and odd characters aren’t miss-interrupted.

All of Magento’s export and import functionality can be found under “System”->”Import/Export”->”Profiles”.

I created a new profile entitled “X-Cart Import” and used the following settings:
xcart-import-magento-profile-settings

Here is the XML profile for my X-Cart import profile as well:

    
    
    true
    
    1
    
    catalog/convert_adapter_product
    parse

One Idea I didn’t realize during my first initial import was the import process at least on a low-end server can take hours to complete, so you may want to take a small portion of your spreadsheet, export to CSV and do a small amount of data to ensure you have all of your data properly laid out. I made this mistake and am now in the process of re-importing all 2000+ products after doing a small amount to ensure things are in order.

If at anytime you want to wipe all of your products and start from no products, run this query in NaviCat or PHPMyAdmin:

TRUNCATE TABLE `catalog_product_bundle_option`;
TRUNCATE TABLE `catalog_product_bundle_option_value`;
TRUNCATE TABLE `catalog_product_bundle_selection`;
TRUNCATE TABLE `catalog_product_entity_datetime`;
TRUNCATE TABLE `catalog_product_entity_decimal`;
TRUNCATE TABLE `catalog_product_entity_gallery`;
TRUNCATE TABLE `catalog_product_entity_int`;
TRUNCATE TABLE `catalog_product_entity_media_gallery`;
TRUNCATE TABLE `catalog_product_entity_media_gallery_value`;
TRUNCATE TABLE `catalog_product_entity_text`;
TRUNCATE TABLE `catalog_product_entity_tier_price`;
TRUNCATE TABLE `catalog_product_entity_varchar`;
TRUNCATE TABLE `catalog_product_link`;
TRUNCATE TABLE `catalog_product_link_attribute`;
TRUNCATE TABLE `catalog_product_link_attribute_decimal`;
TRUNCATE TABLE `catalog_product_link_attribute_int`;
TRUNCATE TABLE `catalog_product_link_attribute_varchar`;
TRUNCATE TABLE `catalog_product_link_type`;
TRUNCATE TABLE `catalog_product_option`;
TRUNCATE TABLE `catalog_product_option_price`;
TRUNCATE TABLE `catalog_product_option_title`;
TRUNCATE TABLE `catalog_product_option_type_price`;
TRUNCATE TABLE `catalog_product_option_type_title`;
TRUNCATE TABLE `catalog_product_option_type_value`;
TRUNCATE TABLE `catalog_product_super_attribute`;
TRUNCATE TABLE `catalog_product_super_attribute_label`;
TRUNCATE TABLE `catalog_product_super_attribute_pricing`;
TRUNCATE TABLE `catalog_product_super_link`;
TRUNCATE TABLE `catalog_product_enabled_index`;
TRUNCATE TABLE `catalog_product_website`;
TRUNCATE TABLE `catalog_product_entity`;
TRUNCATE TABLE `cataloginventory_stock`;
TRUNCATE TABLE `cataloginventory_stock_item`;
TRUNCATE TABLE `cataloginventory_stock_status`;
insert  into `catalog_product_link_type`(`link_type_id`,`code`) values (1,'relation'),(2,'bundle'),(3,'super'),(4,'up_sell'),(5,'cross_sell');
insert  into `catalog_product_link_attribute`(`product_link_attribute_id`,`link_type_id`,`product_link_attribute_code`,`data_type`) values (1,2,'qty','decimal'),(2,1,'position','int'),(3,4,'position','int'),(4,5,'position','int'),(6,1,'qty','decimal'),(7,3,'position','int'),(8,3,'qty','decimal');
insert  into `cataloginventory_stock`(`stock_id`,`stock_name`) values (1,'Default');

(thanks to Mike Smullin on the Magento Forums for this query!)

Whats planned? I am also going to be attempting to apply the same method for importing X-Cart customer data into Magento. As well as making product options and categories importable as well.

There is a good thread on importing product’s in Magento’s community discussing this topic as well.