On my recent project, I work with AfterShip to track the shipment status of WooCommerce order. If you try to find it on Google, you will find this plugin :

Aftership – WooCommerce Tracking

That plugin is pretty cool, you can input the tracking number from wp-admin area and also put the Track button on the order details page so that your customer will be able to track their package with just single click.

But my challenge was bigger than that. I had to find a way to track the shipment automatically, so every time our customer open their order, they will see the latest status of their package, for example : In Transit, Delivered, etc.

Besides that, I also had to create an additional actions when the package being delivered. So I really need a trigger.

How to achieve that goals?

First, I had to sign up for AfterShip Premium account. This way, I will get access to use their webhook trigger.

Webhook is used to ask AfterShip to push back any updates to your system. This is used when the store wants to send out notifications by themselves or receive the latest delivery status / checkpoint. Unlike using API (Get tracking), using webhook is a more efficient way to get delivery updates as webhook asks AfterShip to push back instead of the store keep on asking AfterShip for any updates. Users can select when to ask AfterShip pushes back delivery updates when using webhook. – AfterShip Docs

For general documentation, you can check it here.

There is no specific information about how to do it in WordPress, so I decided to create it myself. For anyone who have same problem with me, I will share the code snippets here. Here are what you need to do :

Setting up AfterShip Webhook

  • Log in to your AfterShip account. Go to Settings -> Triggers -> Webhook.
  • Set your webhook URL.
    For example: http://yoursite.com/?webhook-listener=trackship
  • Choose version, in this example, we will use version 4.
  • Choose status, in this example, we will use Any Updates.
  • Save it!

Next, open your functions.php file (it’s inside your theme or child-theme directory). Put this code and then save the file.

//Aftership Automatic Tracker
function trackship_catcher() {
if(isset($_GET['webhook-listener']) && $_GET['webhook-listener'] == 'trackship') {
//Get and decode JSON
$str = file_get_contents("php://input");
$json = json_decode($str, true);
//Extract Required Variables
$trackingnumber = $json['msg']['tracking_number'];
$shipmentstatus = $json['msg']['tag'];
$shiporderid = $json['msg']['order_id'];
//Update to the latest shipment status
update_post_meta( $shiporderid, '_shipment_status', $shipmentstatus );
//Check if shipping status = InTransit, set order status to complete
if($shipmentstatus == 'InTransit'):
$order = wc_get_order( $shiporderid );
$order->update_status( 'completed' );
view raw functions.php hosted with ❤ by GitHub

That’s it! Now let me explain the codes.

Snippet Explanation

First, on line 5, we detect the webhook-listener parameter with this :


If it’s not empty AND the value equal to trackship, then do the next process.

Next, on the line 7 – 8, we’re trying to get the content from AfterShip webhook and then decode the JSON. You can check the JSON format on this documentation.

After we decode the JSON, on line 12 – 14 we’re trying to get the required variables. In this example, I only get 3 variables :

$trackingnumber = $json['msg']['tracking_number'];
$shipmentstatus = $json['msg']['tag'];
$shiporderid = $json['msg']['order_id'];

Here is the complete variables.

Next, on line 17, we’re updating the post meta to the latest shipment status. In order to get the value, you can refer to this WordPress documentation : get_post_meta()

Line 20 – 22 is just an optional. For example, if you want to do something when the item is In Transit, you can refer to my code sample. 🙂

Hope this useful for you. If you have any question, do not hesitate to leave your comment bellow.