Create Order Tracking Module in Magento



In this Post I am trying to describe how we can create the order tracking modules?. It is very simple and you can create it by using the steps given below:

Step 1. Firts of all we should create the Web Service Role and the Web Service User, For creating the webservice role navigate to the System–> Web Services –> SOAP/XML-RPC- Roles Click on the “Add New Role” Button and Fill the Role Name Here I have taken “Order Tracking”

Magento

Now assign the Resource to this Role Just Select the All from the Role Resources from the right hand side panel as per the screen shot below

Magento

Clicks on the Save Role Buttons on the top right. Now your user role has been added.

Step 2. Create the API User, For creating the webservice user navigate to the System–> Web Services –> SOAP/XML-RPC- Users Click on the “Add New User” Button and Fill the User Name, First Name, Last Name, Email, API Key, API Key Confirmation and This account is Active. Refer to the Screenshot below for all these information all are the dummy data you can change it in your application. Be care full regarding the API Key and note it down because we will use that user name and API key into the Code. Here I have putted the API key as “12457895”

Magento

Now click on the User Role Tab in Left Sidebar and assign the Role to this user and click on the “Save User” button on the Top Right as per the Screenshot given below:

Magento

Step 3. Now Our Web Services User is ready and Create the order-tracking-api.phtml file into the app/design/frontend/base/default/template/cms or you can create this file in your custom template as well app/design/frontend/default/Your_Theme_Name/template/cms

Insert the below code:

<?php if(isset($_GET['order_id'])) { ?>
<div><?php

$live = false;
$error = '';
$statusMessage = '';
$trackingNumberMessage = '';
$shippingMessage = '';

$orderID = '';
$emailAddress = '';

if(isset($_GET['order_id'])) {
	
	$orderID = trim(preg_replace('/[^0-9]*/', '', $_GET['order_id']));
	$billing_zip_code = trim($_GET['billing_zip_code']);
	
	try {
		
		ini_set("soap.wsdl_cache", "0");
		ini_set("soap.wsdl_cache_enabled", "0");
	
		//**************************************/
		
		// change to match your domain name
		$proxy = new SoapClient('http://localhost/magentonew/index.php/api/soap/?wsdl');
		
		//change to your API username/password
		//Webservice User ID
		//Webservice User Password

		$sessionId = $proxy->login('ewatracking', '12457895');
		
	//*********************************************/
		
		//find all orders related to this id
		$orderById = $proxy->call($sessionId, 'sales_order.info', $orderID);			
		
		$items = $orderById['items'];
		
		if($orderById['billing_address']['postcode'] == $billing_zip_code) {
			//we are setting this variable for later use
			$orderLookup = "success";
			if (strtolower($orderById['status']) == "holded") {
				$orderById['status'] = "On Hold";
			}
			
			$statusMessage = "<p style='font-size:16px;'>Order Number: ".$_GET['order_id']."</p>";
			
			$statusMessage .= '<p style="font-size:16px;"><span>Your order status is: <strong>'.ucwords(str_replace('_', ' ', $orderById['status'])).'</strong></span></p>';
			
			if(ucwords(str_replace('_', ' ', $orderById['status'])) == "Processing"){
				$statusMessage .= '<br/><br/><strong>What does this mean?</strong><br/>Processing Time is the time it takes from when you submit your order to when the product leaves the Distribution Center.';
			}
			
			$statusMessage .= "<p style='font-size:16px;'>Order Date: ".$orderById['created_at']."</p>";
			$statusMessage .= "<p style='font-size:16px;'>Order Amount: ".Mage::helper('core')->currency($orderById['base_grand_total'], true, false)."</p>";
			
		} else {
			$orderLookup = "failure";
			echo "<p style='font-weight:bold;font-size:16px;'>We were unable to find your order information. Please verify your Order Number and Zip Code are correct.</p>";
		}
		
			
		//if the order status is complete we look up shipping information
		if(strtolower($orderById['status']) == "complete" && $orderLookup == "success") {
			

			//we look for all shipments related to this order id using internal magento order id
			$findShipments = $proxy->call($sessionId, 'sales_order_shipment.list', array(array('order_id'=>array('like'=>$orderById['order_id']))));
			
			if (count($findShipments) < 1) { //if $findShipments is not an array
				
				echo "<p style='font-weight:bold;font-size:16px;'>There was an unknown error and your shipment information could not be found. Please contact Customer Service to get the current status of your order.</p>";
				
			} else {
			
				//we pull the increment id for the shipment
				$thisShipmentID = $findShipments[0]['increment_id'];
				
				//now we pull all shipment info that specific shipment id
				$getShipmentInfo = $proxy->call($sessionId, 'sales_order_shipment.info', $thisShipmentID);
				$allowedCarriers = $proxy->call($sessionId, 'sales_order_shipment.getCarriers', $thisShipmentID);
				
				//set each variable
				$shipDate = $getShipmentInfo['created_at'];
				$service = $allowedCarriers[$getShipmentInfo['tracks'][0]['carrier_code']];
				if($service == "Custom Value") $service = "Truck Freight";
				$trackingNumber = $getShipmentInfo['tracks'][0]['number'];
												
				$defaultTimeZone = date_default_timezone_get();
				
				date_default_timezone_set('EST');
				
				//print_r($getShipmentInfo);
				
				if($service == "ups") {
					$trackingNumberMessage = "Tracking Number: <strong><a href='http://wwwapps.ups.com/WebTracking/track?loc=en_US&trackNums=".$trackingNumber."' target='_blank'>".$trackingNumber."</a></strong>";
				} else {
					$trackingNumberMessage = "Tracking Number: <strong>".$trackingNumber."</strong>";
				}
				
				//and echo the data to screen
				$shippingMessage = "Your order was shipped on " . date("l, F jS, Y \\a\\t g:i:s a T", strtotime($shipDate . ' ' . $defaultTimeZone)) . " via " . $service . ".<br/><br/>";
				
								
			} //no errors
		
		}
		
		
		if($orderLookup != "failure"){
		
			echo '<p style="font-size:16px;">'.$statusMessage.'<br/>'.$trackingNumberMessage.'</p>';
			echo "<p>".$shippingMessage."</p>";
			echo "<h4>Products in your order:</h4><ul>";
					foreach($items as $item){
						echo "<li>".number_format($item['qty_invoiced'], 0) . " x <strong>" . strtoupper($item['sku']) . "</strong> " . $item['name'] . "</li>";
					}
			echo "</ul>";
		}
		
	} catch (SoapFault $fault) {
		

		if($fault->faultcode == "100") {
			echo "<p style='font-weight:bold;font-size:16px;'>Order Number not found in our system.</p>";
		} elseif ($fault->faultcode == "http") {
			echo "<p style='font-weight:bold;font-size:16px;'>Request timed out. Please try again later.</p>";
		} else {
			//leave this on for testing so we can see SOAP status codes; turn off for live
			if ($live == false) {
				echo "<p style='font-weight:bold;font-size:16px;'>Error $fault->faultcode: $fault->faultstring</p>";
			} else {
				echo "<p style='font-weight:bold;font-size:16px;'>There was an unknown error. Please try again later, or contact us.</p>";
			}
		}			
	}
	
	
	
} // end if

?>
</div>
<?php } ?>

<div style="border:1px solid #DEDDDD;padding:30px;width: 93.5%;">
<p>Please enter the details below to track the status of your order.</p>
<form name="" action="<?php echo $this->getUrl('order-tracking');?>" method="GET">
<div>
	
	<div style="width:100%">
		<div style="float:left;padding-right:150px;padding-bottom: 10px;"><label class="required" for="order_id"><strong><?php echo $this->__('Order Number#') ?></strong>&nbsp;<em>*</em></label></div>
		<div style="padding-bottom: 10px;"><input type="text" name="order_id" id="order_id" value="<?php echo (isset($_GET['order_id'])) ? $_GET['order_id'] : ''; ?>" /></div>
		</div>

	<div style="width:100%">
		<div style="float:left;padding-right:76px;padding-bottom: 10px;"><label class="required" for="billing_zip_code"><strong><?php echo $this->__('Zip Code (Billing Information)') ?></strong>&nbsp;<em>*</em></label></div>
		<div style="padding-bottom: 10px;"><input type="text" name="billing_zip_code" id="billing_zip_code" value="<?php echo (isset($_GET['billing_zip_code'])) ? $_GET['billing_zip_code'] : ''; ?>" /></div>
	</div>
	
	<span class="create_act">
		<button type="submit" title="<?php echo $this->__('Submit') ?>" class="button"><span><span><?php echo $this->__('Submit') ?></span></span></button>
	</span>
</div>
</form>
</div>

Step 4. Now Create the CMS page for displaying the Order Tracking Form, for this Navigate to the CMS –> Pages –> Add New Page Create New page Here I have created the page named as Order Tracking with the Url Key Is : “order-tracking” and Just put the code below into the Content box

{{block type=”core/template” template=”cms/order-tracking-api.phtml”}}

Click to Save Page Button and Save This Page. Wants to Read More about how to Create the Static Page and How to Call the Page through Static page In Magento?

Step 5. Now Call this page through the Frontend Url just use the “order-tracking” because I have created the Page with the Url order-tracking, You can change this one with your page url, The Order tracking form will look like as below:

Magento

Step 6. Now create one test order and just note the Order Number# and the Zip/Pin Code Number For this one Here I have the
Order Number# : 100000001
and The Zip/Post Code : 90011 and the Result will look like as the Given Screenshot below:

Magento

Now your Order Tracking Module is ready. Please Share the Post if you like. Thanks and Enjoy The Magento Coding.