Magento 使用 api实现track Order 订单跟踪

作者:Jeen 发布于:2013-4-21 19:46 Sunday 分类:工作笔记

Magento 使用 api实现track Order  订单跟踪 主要是针对1.5及以下的版本,因为在1.6中已经集成了订单查询的功能了。

先介绍下使用说明吧(涉及一个代码文件,稍后贴出来)

进入后台,
System——>Web Service
新建一个api用户 (记住用户名密码)
(用户角色赋予 Order相关的权限)

新建一个cms页面用于Order查询
调用代码
{{block type=”core/template” template=”cms/order-status-api.phtml”}}

修改order-status-api.phtml 文件中的
$proxy = new SoapClient(‘http://www.magentoeasy.com/index.php/api/soap?wsdl’);
域名为目标网站域名

修改如下代码中的用户名密码为新建的api 用户名密码
//change to your API username/password
$sessionId = $proxy->login(‘orderviewer’, ‘orderviewer’);
OK至于cms页面的链接要怎么放,样式修改,以及新建api用户新建页面清理缓存之类的这边就略过了,需要的可以参考下早期的博文

下面是 order-status-api.phtml 文件的源码

<style type="text/css">
.orderStatusTable {
    border:1px solid #CCCCCC;
    font-size:11px;
}
.track-order-table td { padding:3px;}
.orderStatusTable td {
padding:8px;
}
.currentStatus {
    font-size: 11px;
    background-color: #eee;
    padding: 10px;
    margin: 0 0 15px 0;
    line-height: 15px;
    border: 1px solid #ccc;
    color: #333;
}
.currentStatus span {
    font-size: 14px;
    line-height: 23px;
    color: #000;
}
</style>
<div>
    <h3>Check My Order Status</h3>
</div>
 
<p>Please enter your order number and email address to see the status of your order.</p>
 
<form name="" action="<?php echo $this->getUrl('track-order');?>" method="get">
<table class="track-order-table" border="0" cellspacing="0" cellpadding="0">
    <tr style="padding:3px 0;display:block;">
        <td><strong>Order Number:</strong></td>
        <td><input type="text" name="order_id" id="order_id" value="<?php echo (isset($_GET['order_id'])) ? $_GET['order_id'] : ''; ?>" /></td>
    </tr>
    <tr style="padding:3px 0;display:block;">
        <td><strong>Email Address:</strong></td>
        <td><input name="email_address" type="text" id="email_address" value="<?php echo (isset($_GET['email_address'])) ? $_GET['email_address'] : ''; ?>" size="30" /></td>
    </tr>
    <tr style="padding:5px 0;display:block;clear:both;margin-top:10px;">
        <td> </td>
        <td><button class="button" title="Subscribe" type="submit"><span><span>Submit</span></span></button></td>
    </tr>
</table>
 
</form>
 
<div></div>
 
<?php
 
    $live = true; //determines verbosity of errors
 
    $error = '';
    $statusMessage = '';
    $trackingNumberMessage = '';
    $shippingMessage = '';
 
    $orderID = '';
    $emailAddress = '';
 
    if(isset($_GET['order_id'])) {
 
        //$orderID = trim(preg_replace('/[^0-9]*/', '', $_GET['order_id']));
        $orderID = trim(preg_replace('/[^0-9a-zA-Z]*/', '', $_GET['order_id']));
        $emailAddress = trim($_GET['email_address']);
        try {
 
            ini_set("soap.wsdl_cache", "0");
            ini_set("soap.wsdl_cache_enabled", "0");
 
            //******************************************************************/
 
            // change to match your domain name
            //$siteapiurl= $this->getUrl('index.php/api/soap?wsdl');
            //$proxy = new SoapClient($siteapiurl);
            $proxy = new SoapClient('http://www.magentoeasy.com/index.php/api/soap?wsdl');
            //change to your API username/password
            $sessionId = $proxy->login('orderviewer', 'orderviewer');
 
            //******************************************************************/
 
            //find all orders related to this id
            $orderById = $proxy->call($sessionId, 'sales_order.info', $orderID);
            //print_r($orderById);
            //echo "<hr>";
            $items = $orderById['items'];
            //print_r($items);
            if($orderById['customer_email'] == $emailAddress) {
                //we are setting this variable for use later
                $orderLookup = "success";
                if (strtolower($orderById['status']) == "holded") {
                    $orderById['status'] = "On Hold";
                }
                $statusMessage = '<span>Your order status is: <strong>'.ucwords(str_replace('_', ' ', $orderById['status'])).'</strong></span>';
 
                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.';
                }
 
            } else {
                $orderLookup = "failure";
                echo "We were unable to find your order information. Please verify your Order Number and Email Address are correct.";
            }
            //echo $orderById['status']."<hr>";
            //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']))));
                //print_r($findShipments);
                if (count($findShipments) < 1) { //if $findShipments is not an array
 
                    echo "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.";
 
                } else {
 
                    //we pull the increment id for the shipment
                    $thisShipmentID = $findShipments[0]['increment_id'];
 
                    //now we pull all shipment info that specific shipment id
                    if(!$proxy->call($sessionId, 'sales_order_shipment.info', $thisShipmentID)){
                        $trackingNumberMessage = "Shipment ID: <strong>".$thisShipmentID."</strong>";
                        $shippingMessage = "Your order was shipped on " . $findShipments[0]['created_at'] . ".<br/><br/>";
                    }
                    else {
                        $getShipmentInfo = $proxy->call($sessionId, 'sales_order_shipment.info', $thisShipmentID);
                        //print_r($getShipmentInfo);
                            //set each variable
                        $shipDate = $getShipmentInfo['created_at'];
                        $defaultTimeZone = date_default_timezone_get();
                        date_default_timezone_set('EST');
                        //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)) .".<br/>";
                        if(count($getShipmentInfo['tracks']) > 0){
                            foreach($getShipmentInfo['tracks'] as $temp_track)
                            $shippingMessage .= "Shipped By : ".$temp_track['title']."  ,Tracking Number :".$temp_track['number']."<br/>"; }
                            $shippingMessage .= 'Check your shipment state by tracking number~.<br>By Fedex : <a target="_blank" href="http://www.fedex.com/Tracking">Fedex Express</a><br/>
By DHL : <a target="_blank" href="http://www.dhl.com/en/express/tracking.html">DHL</a><br/>
By UPS : <a target="_blank" href="http://www.ups.com/tracking/tracking.html">UPS</a><br/>';
                        }
                        $shippingMessage .= "<br/>";
                    }
 
                } //no errors
 
            }
 
            if($orderLookup != "failure"){
 
                echo '<p style="padding: 10px; background:#eee; margin: 10px 0;">'.$statusMessage.'<br/>'.$trackingNumberMessage.'</p>';
 
                echo $shippingMessage;
 
                echo "<h4>Products in your order:</h4><ul>";
                        foreach($items as $item){
                            $temp_sku = $item['sku'];
                            if(strpos($temp_sku,'-'))
                            {
                                $temp_sku=substr($temp_sku,0,strpos($temp_sku,'-'));
                            }
                            echo "<li>".number_format($item['qty_invoiced'], 0) . " x <strong>" . strtoupper($temp_sku) . "</strong> " . $item['name'] . "</li>";
                        }
                echo "</ul>";
            }
 
        } catch (SoapFault $fault) {
            //this is a basic implementation of error checking. I am using try to stop the error from showing a nasty magento error page
            if($fault->faultcode == "100") {
                echo "That Order Number was not found in our system.";
            } elseif ($fault->faultcode == "http") {
                echo "Request timed out. Please try again later.";
            } else {
                //leave this on for testing so we can see SOAP status codes; turn off for live
                if ($live == false) {
                    echo "Error $fault->faultcode: $fault->faultstring";
                } else {
                    echo "Error $fault->faultcode: $fault->faultstring"."<hr>";
                    //echo "There was an unknown error. Please try again later, or contact us.";
                }
            }
        }
 
    } // end if
 
?>
<p><br /><br /><em>For detailed information regarding the status of your order, please contact our helpful Customer Service Experts.</em></p>

暂记如此,欢迎留言交流 :-)

标签: magento

发表评论:

©2010-2024 Jeen All Rights Reserved.Powered by emlog 京ICP备15058100号-1