View Single Post
  #1  
Old 04-13-2013, 09:58 PM
  Mish's Avatar 
Mish Mish is offline
 

Advanced Member
  
Join Date: Feb 2008
Posts: 92
 

Thumbs up Australia Post - Automatic Order 'Completion' when Delivered

Hi guys,

I've developed a short PHP script that will automatically changed an order status from QUEUED or PROCESSED to COMPLETE once the package has been delivered to the customer.

Note that it will not send an email to the customer or you (if you have that configured).

PRE-REQUISITES TO WORK:
You must enter your Australia Post shipping number in the Tracking number field on the order page.




The script not only automatically updates your order status, but can be loaded in your webbrowser to see a shipping summary:

http://s2.postimg.org/9t6lc5w7t/Screen_Shot_2013_04_14_at_6_16_44_PM.png

Code:
<?php function fetchOrders() { //echo"Pre connect"; mysql_connect(localhost,'<DATABASE USERNAME>','<DATABASE USER PASSWORD>'); //echo "attempting to select database"; @mysql_select_db('<XCART DATABASE NAME>') or die( "Unable to select database"); $query="SELECT * FROM xcart_orders where status in ('P','Q') and LENGTH(tracking) > 3"; //echo "Executing query"; $result=mysql_query($query); $num=mysql_numrows($result); return $result; mysql_close(); } function checkOrderStatus() { $orders = fetchOrders(); $num=mysql_numrows($orders); $i = 0; if ($num > 0) { echo ('<table border="1"> <tr> <th style="background-color:blue; color: gold">Order ID</th> <th style="background-color:blue; color: gold">Customer</th> <th style="background-color:blue; color: gold">Status</th> <th style="background-color:blue; color: gold">Date</th> <th style="background-color:blue; color: gold">Location</th> <th style="background-color:blue; color: gold">Destination</th> <th style="background-color:blue; color: gold">Shipping Type</th> </tr>'); } while ($i < $num) { $id = mysql_result($orders,$i,"orderid"); $trackingNumber = mysql_result($orders,$i,"tracking"); $destination = strtoupper(mysql_result($orders,$i,"s_city")); $s_state = strtoupper(mysql_result($orders,$i,"s_state")); $shippingType = mysql_result($orders,$i,"shipping"); $customer = mysql_result($orders,$i,"b_lastname"); //echo $id . ": " . $trackingNumber; if (isDelivered($trackingNumber, $lastStatus, $lastDate, $lastLocation)) { mysql_connect(localhost,'<DATABASE USERNAME>','<DATABASE USER PASSWORD>'); //echo "attempting to select database"; @mysql_select_db('<XCART DATABASE NAME>') or die( "Unable to select database"); //echo " CLOSING ORDER #" . $id ; $query="UPDATE xcart_orders SET status='C' WHERE orderid='$id'"; //echo " | " . $query; mysql_query($query); echo " >>> " . mysql_affected_rows() . " <<< "; echo mysql_error(); mysql_close(); } else echo ("<tr><td>{$id}</td><td>{$customer}</td><td>{$lastStatus}</td><td>{$lastDate}</td><td>{$lastLocation}</td><td>{$destination}, {$s_state}</td><td>{$shippingType}</td></tr>"); $i++; } if ($num > 0) echo ('</table>'); echo $num . " orders currently in shipment"; } function get_content($url) { $ch = curl_init(); $fakeAgent = 'IE 6 - Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322'; curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_USERAGENT,$fakeAgent ) ; curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); ob_start(); curl_exec ($ch); curl_close ($ch); $string = ob_get_contents(); ob_end_clean(); return $string; } function isDelivered($trackingNumber, &$lastStatus, &$lastDate, &$lastLocation) { $content = get_content('http://auspost.com.au/track/track.html?id=' . $trackingNumber); $doc = new DOMDocument(); @$doc->loadHTML($content); $cells = $doc->getElementsByTagName('td'); foreach ($cells as $cell){ $event[] = $cell->nodeValue; } $date = $event[0]; $status = $event[1]; $location = $event[2]; if ($status == "Delivered") return true; else { $lastStatus = strlen($status) > 1 ? $status : "No Tracking Information"; $lastDate = strlen($date) > 1 ? $date : "N/A"; $lastLocation = strlen($location) > 1 ? $location : "N/A"; foreach ($event as $e) { if ($e == "Delivered") return true; } } return false; } checkOrderStatus(); //echo $tbody->length; ?>
I called the file AP-Delivery.php , and placed it in my home www directory.

Once you have put the file there and edited it to have your database name, database username and password, you have to configure a CRON JOB to run every 30 minutes.

In cPanel this is down the bottom under the advanced tab.

1> Click on CRONJOBS
2> Under ADD NEW CRONJOB, select the COMMON SETTINGS dropbox and choose "Twice an hour".
3> In the COMMAND text entry field, type the following:

php -f /home/<USERNAME>/public_html/AP-Delivery.php >/dev/null 2>&1

Where the username is your cPanel username.

The script will run every half hour. It will look for orders that have a TRACKING NUMBER and have a status of PROCESSED or QUEUED. It will go to the Australia Post website and check whether the order has been delivered. If it has, it will (directly by accessing the back end database) change the status to COMPLETE.

To load the shipping summary, just go to https://yourdomain.com//AP-Delivery.php

Hope this helps.
__________________
X-Cart Gold 4.6.1
Reply With Quote