Skip to content

Instantly share code, notes, and snippets.

@hendcorp
Last active October 19, 2021 11:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hendcorp/d010833a75954c244a41663847ca5389 to your computer and use it in GitHub Desktop.
Save hendcorp/d010833a75954c244a41663847ca5389 to your computer and use it in GitHub Desktop.
AfterShip webhook for WordPress
<?php
//Aftership Automatic Tracker
add_action('wp_head','trackship_catcher');
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' );
endif;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment