Unlike importing customers orders has only one way to import i.e. via API Request. The very first thing we require is to export orders from Magento. Then we will write script to read the exported csv file and make API request to create order in shopify.

Exporting orders from Magento

There isn’t a specific way to export order but from my research i found this module to be very effective in exporting orders in detail. Module : Bulk Import+Export Orders to CSV. The sample this module provides is here. This module exports order in detail we require in shopify.

Importing orders in shopify

Script to retrieve information from the exported csv is below. It can be in any format as i am using the exported CSV from the module mentioned above. The logic is reading CSV file and accumulating all the required information in formatted array.

$file = fopen("example.csv","r");

$product = array();
$final_arr = array(); $j=-1;$i=0;
	while (($line = fgetcsv($file)) !== FALSE) {
	  	//$line is an array of the csv elements
	  	if($line[0]!='order_id'){
	  		if($line[0]!=''){ $j++; $i=0;
	  			$product[$j]['order_id'] = $line[0];
	  			$product[$j]['email'] = $line[1];
	  			$product[$j]['billing_prefix'] = $line[63];
			    $product[$j]['billing_firstname'] = $line[64];
			    $product[$j]['billing_middlename'] = $line[65];
			    $product[$j]['billing_lastname'] = $line[66];
			    $product[$j]['billing_suffix'] = $line[67];
			    $product[$j]['billing_street_full'] = $line[68];
			    $product[$j]['billing_city'] = $line[69];
			    $product[$j]['billing_region'] = $line[70];
			    $product[$j]['billing_country'] = $line[71];
			    $product[$j]['billing_postcode'] = $line[72];
			    $product[$j]['billing_telephone'] = $line[73];
			    $product[$j]['billing_company'] = $line[74];
			    $product[$j]['billing_fax'] = $line[75];

			    $product[$j]['shipping_prefix'] = $line[77];
			    $product[$j]['shipping_firstname'] = $line[78];
			    $product[$j]['shipping_middlename'] = $line[79];
			    $product[$j]['shipping_lastname'] = $line[80];
			    $product[$j]['shipping_suffix'] = $line[81];
			    $product[$j]['shipping_street_full'] = $line[82];
			    $product[$j]['shipping_city'] = $line[83];
			    $product[$j]['shipping_region'] = $line[84];
			    $product[$j]['shipping_country'] = $line[85];
			    $product[$j]['shipping_postcode'] = $line[86];
			    $product[$j]['shipping_telephone'] = $line[87];
			    $product[$j]['shipping_company'] = $line[88];
			    $product[$j]['shipping_fax'] = $line[89];

			    $product[$j]['shipping_method'] = $line[28];
			    $product[$j]['shipping_price'] = $line[29];

			    $product[$j]['tax'] = $line[14];
			    $product[$j]['order_status'] = $line[46];
			    $product[$j]['base_total_paid'] = $line[54];
			    $product[$j]['base_grand_total'] = $line[33];
			    $product[$j]['currency']=$line[52];
			    $product[$j]['discount']=$line[16]; //base_discount_amount
			    $product[$j]['coupon_code']=$line[25]; //coupon_code
			    $product[$j]['created_at']=$line[8];

	  			//array_push($customer,$line);
	  			$product[$j]['items'][$i] = array('title'=>$line[92],'quantity'=>$line[93],'price'=>$line[105],'sku'=>$line[91]);
	  		}else{
	  			$product[$j]['items'][$i] = array('title'=>$line[92],'quantity'=>$line[93],'price'=>$line[105],'sku'=>$line[91]);
	  		}
	  	}else{
	  		//echo '<pre>';
	  		//print_r($line);
	  	}
	  	$i++;
	}
echo '<pre>';
print_r($product);

The above code fetches all the required information we require to import in shopify. The printr of above gives the data in below order.

Array
(
    [0] => Array
        (
            [order_id] => 100000024
            [email] => Leslie@mailinator.com
            [billing_prefix] => 
            [billing_firstname] => Leslie
            [billing_middlename] => 
            [billing_lastname] => H
            [billing_suffix] => 
            [billing_street_full] => 3333 W Commercial Blvd
STE 202
            [billing_city] => Fort Lauderdale
            [billing_region] => Florida
            [billing_country] => US
            [billing_postcode] => 33309
            [billing_telephone] => 866-948-8384
            [billing_company] => 
            [billing_fax] => 866-948-8394
            [shipping_prefix] => 
            [shipping_firstname] => Leslie
            [shipping_middlename] => 
            [shipping_lastname] => H
            [shipping_suffix] => 
            [shipping_street_full] => 3333 W Commercial Blvd
STE 202
            [shipping_city] => Fort Lauderdale
            [shipping_region] => Florida
            [shipping_country] => US
            [shipping_postcode] => 33309
            [shipping_telephone] => 866-948-8384
            [shipping_company] => 
            [shipping_fax] => 866-948-8394
            [shipping_method] => Flat Rate - Fixed
            [shipping_price] => 15
            [tax] => 0
            [order_status] => complete
            [base_total_paid] => 327
            [base_grand_total] => 327
            [currency] => USD
            [discount] => 0
            [coupon_code] => 
            [created_at] => 2014-06-04 07:26:31
            [items] => Array
                (
                    [0] => Array
                        (
                            [title] => Configurable Product
                            [quantity] => 2
                            [price] => 100
                            [sku] => Simple3
                        )

                    [1] => Array
                        (
                            [title] => Simple3
                            [quantity] => 2
                            [price] => 
                            [sku] => Simple3
                        )

                    [2] => Array
                        (
                            [title] => Simple with custom option
                            [quantity] => 1
                            [price] => 112
                            [sku] => Simple2-31
                        )

                )

        )

i.e. we read order from the csv for multiple products and other essential values for shopify import. For eg. billing information, shipping information, multiple products customer has ordered, respective quantity, price and so on.

Now next big thing is to match order status & payment staus from Magento to shopify fulfillment status & financial status.

Convention in Order and payment status (For me it worked you can have different convention as required)

  • If order status from magento is complete, shipped, manufacturing, dispached or processing i mapped them to fulfilled as fulfillment status. else unfulfillment.
  • If base total paid from maento exists then i have mapped them to financial status as paid, else pending.
  • and in case of order status from magento is cancelled, i mapped them to voided as fulfillment status.
  • The discount amount, discount code and inclusive tax of product is planned to have in order notes in shopify.

This mapping is in general, you can have your own kind of mapping.

Below is the code to make api request in shopify. For this i have used shopify app skeleton. The request format can be in any format.

try
	{
		foreach($product as $p){

			//order id track
			//if($p['order_id']>100001885){
				echo $p['order_id'].'<br/>';
				$transaction_amount = $p['base_total_paid'];
				$discount = $p['discount'];
				$coupon_code = $p['coupon_code'];
				//order status handling
				$order_status = $p['order_status'];
				if($order_status=='complete' || $order_status=='shipped' || $order_status=='manufacturing' || $order_status=='dispatched' || $order_status=='processing'){
					$status = 'fulfilled'; //for magento order tatus complete and shipped : fulfilled
				}else{
					$status = 'unfulfilled'; //for others unfulfilled
				}

				//payment status handling : default is paid
				if($p['base_total_paid']==0 || $p['base_total_paid']=''){
					$financial_status = 'pending'; //not paid
				}else{
					$financial_status = 'paid';
				}

				if($order_status=='canceled'){
					$financial_status = 'voided'; //canceled orders as voided
				}

			$note = '';//if discount and code is used keep that as note;
			if($p['tax']){
				$note .= 'Total price is inclusive of tax (Tax total: '.$p['currency'].' $'.$p['tax'].')'; 
			}
			if($discount){
				$note .= ' Discount amount is '.$p['currency'].' $'.$discount*(-1);
			}if($discount && $coupon_code){
				$note .= ' using "'.$coupon_code.'" coupon code.';
			}

			if($p['order_id']){
				$note .= ' Order id from previous site is #'.$p['order_id'].'.';
			}
			$created_date = explode('/', $p['created_at']);
			//print_r($p['created_at']); die();
			$created_at = $created_date[1].'/'.$created_date[0].'/'.$created_date[2];
			# Making an API request can throw an exception
			if($transaction_amount){
				$product = $shopify('POST /admin/orders.json', array(), array
				(
					'order'=>array(
						"email"=> $p['email'],
		    			"fulfillment_status"=> $status,
						"line_items"=> $p['items'],
							"shipping_lines"=>array(
								array(
									"title"=>$p['shipping_method'],
									"price"=>$p['shipping_price'],
								)
							),
							"billing_address"=>array(
							  "first_name"=> $p['billing_firstname'],
						      "last_name"=> $p['billing_lastname'],
						      "address1"=> $p['billing_street_full'],
						        "city"=> $p['billing_city'],
						        "province"=> $p['billing_region'],
						        "phone"=> $p['billing_telephone'],
						        "zip"=> $p['billing_postcode'],
						        "country"=> $p['billing_country']
							),
							"shipping_address"=>array(
							  "first_name"=> $p['shipping_firstname'],
						      "last_name"=> $p['shipping_lastname']==''?$p['billing_lastname']:$p['shipping_lastname'],
						      "address1"=> $p['shipping_street_full'],
						        "city"=> $p['shipping_city'],
						        "province"=> $p['shipping_region'],
						        "phone"=> $p['shipping_telephone'],
						        "zip"=> $p['shipping_postcode'],
						        "country"=> $p['shipping_country']
							),
							"financial_status" => $financial_status,
							//"total_tax"=>$p['tax'],
							"currency"=>$p['currency'],
							"transactions"=>array(
								array("amount"=>$transaction_amount)
							),
							"note"=>$note,
							"created_at"=>$created_at,
					)
				));
		}else{
			$product = $shopify('POST /admin/orders.json', array(), array
			(
				'order'=>array(
					"email"=> $p['email'],
	    			"fulfillment_status"=> $status,
					"line_items"=> $p['items'],
						"shipping_lines"=>array(
							array(
								"title"=>$p['shipping_method'],
								"price"=>$p['shipping_price'],
							)
						),
						"billing_address"=>array(
						  "first_name"=> $p['billing_firstname'],
					      "last_name"=> $p['billing_lastname'],
					      "address1"=> $p['billing_street_full'],
					        "city"=> $p['billing_city'],
					        "province"=> $p['billing_region'],
					        "phone"=> $p['billing_telephone'],
					        "zip"=> $p['billing_postcode'],
					        "country"=> $p['billing_country']
						),
						"shipping_address"=>array(
						  "first_name"=> $p['shipping_firstname'],
					      "last_name"=> $p['shipping_lastname']==''?$p['billing_lastname']:$p['shipping_lastname'],
					      "address1"=> $p['shipping_street_full'],
					        "city"=> $p['shipping_city'],
					        "province"=> $p['shipping_region'],
					        "phone"=> $p['shipping_telephone'],
					        "zip"=> $p['shipping_postcode'],
					        "country"=> $p['shipping_country']
						),
						"financial_status" => $financial_status,
						//"total_tax"=>$p['tax'],
						"currency"=>$p['currency'],
						"note"=>$note,
						"created_at"=>$created_at,

				)
			));
		}
		//}//endif;
		//die('die at first');
		}//endforeach
		
		//print_r($product);
	}

And this above request should create order in shopify. The screenshot below shows the order created in Shopify.

shopify-order-imported-from-magento

 

Ordered date can be very confusing because Shopify processes according to the timezone set on store configuration. If the customer is already imported and account is activated then the customer will be linked, but in my case above the customer is new so there is ‘no account’ level mentioned.

Suman K.C

eCommerce Developer, writes about stuff related to eCommerce development. Currently based in Kathmandu, loves reading about startups & new technology, introvert nature, fond of traveling & playing futsal.

More Posts