WooCommerce AfterShip plugin shipping tracking number sync using easycom.io API into WooCommerce Database

WooCommerce AfterShip plugin shipping tracking number sync using easycom.io API into WooCommerce Database

AfterShip is a WooCommerce-compatible plugin that trackback and syncs user shipping details.

Easycom.io is a multi-channel inventory management and reconciliation software.

What is exciting in this blog post?

We are going to discuss the easycom.io API shipping tracking number shared with the WooCommerce AfterShiping plugin.
Generally, this process needs manual data entry. We are converting this manual process to automation.

Requirements:

  • Must have Easycom.io API credentials and login details
  • AfterShip plugin activated in WordPress admin

Function for easycom API token generate

//echo easyecom_api_token();
function easyecom_api_token(){

    $options = get_option( 'rasa_shipping_tracker_settings' );

	$payload = wp_json_encode( array(  
	"email" => isset( $options['username'] ) ? $options['username'] : '',
	"password" => isset( $options['password'] ) ? $options['password'] : '' ) );
	$api_url = "https://app.easyecom.io/getApiToken";

	$response = wp_remote_post( $api_url, array(
		'headers' => [
			'content-type' => 'application/json'
		],
		'body'      => $payload,
		'timeout'   => 90,
		'sslverify' => false,
	) );
	if ( is_wp_error( $response ) ) {
			
		return false; //$error_message = $response->get_error_message();

	} else {

		$api_data = json_decode( $response['body'], true );
		
		if(isset($api_data['code']) && $api_data['code'] == 200){

			if(isset($api_data['data']['api_token']) && $api_data['data']['api_token'] != ''){
				return $api_data['data']['api_token'];
			} else {
				return false;
			}

		} else{
			return false;
		}
	}

	
}

Function to get shipping tracking number and sync with WooCommerce

function easyecom_api_getShippingInfo($api_token, $order_id){

	$remote_link = add_query_arg(
		array(
			'api_token'   => $api_token,
			'reference_code' => $order_id,
		),
		'https://api.easyecom.io/Carriers/getTrackingDetails'
	);

	$response = wp_safe_remote_get(
		$remote_link,
		array(
			'timeout' => 3,
		)
	);

	$response = wp_remote_retrieve_body( $response );
	//print_r($response);

	if ( is_wp_error( $response ) ) {
			
		return false; //$error_message = $response->get_error_message();

	} else {
		$api_data = json_decode( $response, true );

		//print_r($api_data);
		
		if(isset($api_data['code']) && $api_data['code'] == 200){
			if(isset($api_data['data'][0]['carrierName']) && $api_data['data'][0]['carrierName'] != '' && strlen($api_data['data'][0]['carrierName']) > 0){
				$tracking_number = $api_data['data'][0]['awbNumber'];
				$tracking_provider_name = $api_data['data'][0]['carrierName'];
				$currentShippingStatus = $api_data['data'][0]['currentShippingStatus'];
				$expectedDeliveryDate = $api_data['data'][0]['expectedDeliveryDate'];
				$orderStatus = $api_data['data'][0]['orderStatus'];

            
                if($api_data['data'][0]['carrierName'] == 'ATS'){
                    $tracking_item_slug = strtolower(wc_clean( 'amazon' ));
                }
                else {
                    $tracking_item_slug = strtolower(wc_clean( $api_data['data'][0]['carrierName'] ));
                }

				$tracking_item[0]['slug'] = $tracking_item_slug;
                $tracking_item[0]['tracking_number']   = wc_clean( $api_data['data'][0]['awbNumber'] );
                $tracking_item[0]['tracking_id']       = md5( "{$tracking_item['slug']}-{$tracking_item['tracking_number']}" );
                $tracking_item[0]['additional_fields'] = array(
                    'account_number'      => '', //wc_clean( $args['additional_fields']['account_number'] ),
                    'key'                 => '', //wc_clean( $args['additional_fields']['key'] ),
                    'postal_code'         => wc_clean( $api_data['data'][0]['pin_code'] ),
                    'ship_date'           => '', //wc_clean( $args['additional_fields']['ship_date'] ),
                    'destination_country' => wc_clean( 'India' ),
                    'state'               => wc_clean( $api_data['data'][0]['state'] ),
                );
                $tracking_item[0]['metrics']           = array(
                    'created_at' => current_time( 'Y-m-d\TH:i:s\Z' ),
                    'updated_at' => current_time( 'Y-m-d\TH:i:s\Z' ),
                );

			    $tracking_number_existing = get_post_meta( $order_id, '_aftership_tracking_number', true );
	
				if( $tracking_item_slug != ''){
					if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
						update_post_meta( $order_id, '_aftership_migrated', 'ok' );
						//update_post_meta( $order_id, '_aftership_tracking_items', $tracking_items );
						update_post_meta( $order_id, '_aftership_tracking_number', $tracking_number );
						update_post_meta( $order_id, '_aftership_tracking_provider_name', $tracking_provider_name );
                        update_post_meta( $order_id, '_aftership_tracking_items', $tracking_item );
					} else {
						$order = new WC_Order( $order_id );
						$order->update_meta_data('_aftership_migrated', 'ok' );
						//$order->update_meta_data( '_aftership_tracking_items', $tracking_items );
						$order->update_meta_data( '_aftership_tracking_number', $tracking_number );
						$order->update_meta_data( '_aftership_tracking_provider_name', $tracking_provider_name );
                        $order->update_meta_data( '_aftership_tracking_items', $tracking_item );
						$order->save_meta_data();
					}
				}
				
			}
		} else{
			return false;
		}
	}

}

WooCommerce order notes filter to call function and sync details.

//add the action 
add_action('woocommerce_order_note_added', 'custom_woocommerce_order_note_added', 20, 2);

// define the woocommerce_order_note_added callback 
function custom_woocommerce_order_note_added( $comment_id, $order ){ 
	$comment_obj   = get_comment( $comment_id );
	$customer_note = $comment_obj->comment_content;

	if (  $order->get_status() == 'completed' ) {
		$api_token = easyecom_api_token();
		$order_id = $order->get_id();
		if($api_token != '' && $order_id != ''){
			easyecom_api_getShippingInfo($api_token, $order_id);
		}
	}
} 

How to use this code?

This code needs to place into the currently active theme functions.php file. But I recommend the child theme or adding code in a separate file and including that file in the theme functions.php file.