Skip to content

Instantly share code, notes, and snippets.

@charddude
Last active August 29, 2015 14:04
Show Gist options
  • Save charddude/48929b68fa5ce475124f to your computer and use it in GitHub Desktop.
Save charddude/48929b68fa5ce475124f to your computer and use it in GitHub Desktop.
Simple tracking plugin for WordPress
// Tracking JQuery call
function submit_me(){
jQuery.post(the_ajax_script.ajaxurl, jQuery('#theForm').serialize()
,
function(response_from_tracking_function){
jQuery("#response_area").html(response_from_tracking_function);
}
);
jQuery('#theForm').find('input[type=text]').val('Consignment No');
//jQuery( "deliverysignature" ).wrapInner( "<div><div><p><em><b></b></em></p></div></div>" );
//});
//$( "deliverysignature" ).wrapInner(function() {
// return "<img src=\'" + this.nodeValue + "\' />";
//});
/*var img = $('<img />'); //Equivalent: $(document.createElement('img'))
img.attr('src', response_from_tracking_function.imgurl);
img.appendTo('deliverysignature');
*/
}
/*
Plugin Name: TraceNow Tracking
Plugin URI: http://www.titandesign.co.uk
Description: A simple parcel tracking plugin for Codeway TraceNow.
Author: Richard King
Author URI: http://www.titandesign.co.uk/
Version: 1.0.0
License: GPL-2.0+
License URI: http://www.opensource.org/licenses/gpl-license.php
*/
#response_area {
width: 90%;
margin: 0 auto;
background-color: #eee;
padding: 10px 20px;
color: #000;
}
.tracenow {
max-width: 100%;
}
#theForm{
margin: 0 auto;
margin-bottom: 5%;
max-width: 500px;
}
#consignmentNumber {
max-width: 100%;
border: 1px solid #333;
padding: 3px 5px;
}
#submit_button {
position: relative;
cursor: pointer;
background: blue;
color: white;
padding: 2px 4px;
}
.category {
font-weight: bold;
}
consignmentnumber {
font-size: 24px;
}
consignmentnumber::before {
content: "Consignment Number: ";
color: red;
}
customerref,
itemcount,
name,
address1,
address2,
town,
county,
postcode,
country,
recipient,
status,
collected,
delivered,
collectioncode,
deliverycode,
comments,
created,
deliverylatitude,
deliverylongitude {
font-size: 18px;
line-height: 1.2em;
}
deliverysignature:before {
content: "";
}
deliverysignature:after {
content: "";
}
itemcount::before {
content: "Item quantity: ";
border-bottom: 1px solid #ccc;
color: red;
}
name::before {
content: "Name: ";
border-bottom: 1px solid #ccc;
color: red;
}
address1::before {
content: "Address: ";
border-bottom: 1px solid #ccc;
color: red;
}
recipient::before {
content: "Recipient: ";
border-bottom: 1px solid #ccc;
color: red;
}
created::before {
content: "Created: ";
border-bottom: 1px solid #ccc;
color: red;
}
status::before {
content: "Status: ";
border-bottom: 1px solid #ccc;
color: red;
}
collected::before {
content: "Collected: ";
border-bottom: 1px solid #ccc;
color: red;
}
delivered::before {
content: "Delivered: ";
border-bottom: 1px solid #ccc;
color: red;
}
deliverylatitude::before {
content: "Delivery Latitude: ";
border-bottom: 1px solid #ccc;
color: red;
}
deliverylongitude::before {
content: "Delivery Longitude: ";
border-bottom: 1px solid #ccc;
color: red;
}
#imagediv {
width: 90%;
margin: 0 auto;
}
<?php
/*
Plugin Name: TraceNow Tracking
Plugin URI: http://www.titandesign.co.uk
Description: A simple parcel tracking plugin for Codeway TraceNow.
Version: 4.0
Author: Richard King
Author URI: http://www.titandesign.co.uk/
License: GPL-2.0+
License URI: http://www.opensource.org/licenses/gpl-license.php
*/
// ENQUEUE AND LOCALISE SCRIPTS
wp_enqueue_script( 'my-ajax-handle', plugin_dir_url( __FILE__ ) . 'ajax.js', array( 'jquery' ) );
wp_enqueue_style( 'my-ajax-handle', plugin_dir_url( __FILE__ ) . 'style.css' );
wp_localize_script( 'my-ajax-handle', 'the_ajax_script', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
// THE TRACKING FUNCTION
if( !class_exists( 'WP_Http' ) )
include_once( ABSPATH . WPINC. '/class-http.php' );
function tracking_function(){
//if ( isset( $_POST['consignmentNumber'] ) && '1' == $_POST['consignmentNumber'] ) {
$consignmentNumber = $_POST['consignmentNumber'];
$url = ('http://services.tracenow.net/TraceNowAccess.asmx/GetConsignment?consignmentNumber=' .($consignmentNumber) .'&externalAccessGuid=d5dbffb4-0336-44bf-b72c-00fb9aaac759');
$args = array(
'method' => 'GET',
'timeout' => 45,
'redirection' => 5,
'httpversion' => '1.1',
'content-type' => 'application/x-www-form-urlencoded',
'body' => array(),
'headers' => array(),
'blocking' => true,
'cookies' => array(),
'connection' => 'close',
);
$response = wp_remote_get( $url, $args );
if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
echo "Something went wrong: $error_message";
} else {
/*include ('/render_xml_to_html.php');
if(function_exists('render_xml_data')){
render_xml_data('example_data.xml');
}else{
echo null;
} */
echo '<h3>Consignment Details</h3>' . '<pre>' . print_r($response['body'], true) . '</pre>';// this is passed back to the javascript function
die();
} // end if
//} // end if
}
// THE AJAX ADD ACTIONS
add_action( 'wp_ajax_the_ajax_hook', 'tracking_function' );
add_action( 'wp_ajax_nopriv_the_ajax_hook', 'tracking_function' ); // need this to serve non logged in users
// ADD TRACKING FORM TO THE PAGE
function tracenow_frontend(){
$the_form = '
<form id="theForm" method="POST">
<input id="consignmentNumber" name="consignmentNumber" value="Consignment No" type="text" />
<input name="action" type="hidden" value="the_ajax_hook" /> <!-- this puts the action the_ajax_hook into the serialized form -->
<input id="submit_button" value="Track" type="button" onClick="submit_me();" />
</form>
<div id="response_area">
Your tracking details will appear here
<br />
<div id="imagediv">
</div>
</div>';// END DIV RESPONSE AREA
return $the_form;
}
add_shortcode("tn_ajax_frontend", "tracenow_frontend");
@charddude
Copy link
Author

I've tested the tracking function on its own in a separate php file. It returns the correct details for the consignment number and member guid used. The return is all the tracking details include datetime stamp, address, contact, parcel details and image of the signature.
Can be accessed here to see the xml response:

http://services.tracenow.net//TraceNowAccess.asmx/GetConsignment?consignmentNumber=lc0614061377&memberGuid=d5dbffb4-0336-44bf-b72c-00fb9aaac759 HTTP/1.1

Using the plugin the only return I get is 0. Obviously at the moment theres no installer so the plugin needs to be copied to the plugins folder manually. It will work on a page by adding the shortcode [tn_ajax_frontend].
Tracking number to use is: lc0614061377
If someone could have a look and point me in the right direction it would be gratefully appreciated.

Many thanks

Richard

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment