Skip to content

Instantly share code, notes, and snippets.

@vanbo
Created October 17, 2018 11:46
Show Gist options
  • Select an option

  • Save vanbo/6fd3226ae8fe17200141339674874816 to your computer and use it in GitHub Desktop.

Select an option

Save vanbo/6fd3226ae8fe17200141339674874816 to your computer and use it in GitHub Desktop.
Linnworks - Transfer Tracking code to plugin
/**
* Adds hooks on the rest init
*/
function prefix_filter_order_fields() {
// Rest API update of the order
add_filter( "woocommerce_rest_pre_insert_shop_order_object", 'prefix_transfer_tracking_info_to_format', 5, 2 );
}
add_action( 'rest_api_init', 'prefix_filter_order_fields', 0 );
/**
* Transfers the tracking meta coming linnworks to the linnworks plugin meta
* @param WC_Order $order
* @param $request
*
* @return mixed
*/
function prefix_transfer_tracking_info_to_format( $order, $request ) {
$shipping_service = $order->get_meta( 'linnworks_tracking_provider', true );
$tracking_number = $order->get_meta( 'linnworks_tracking_number', true );
$order->add_meta_data( 'linn_shipping_service_name', $shipping_service, true );
$order->add_meta_data( 'linn_shipping_tracking_number', $tracking_number, true );
// NOTE: You can save the order here or just return the order, it will be saved later as well
// $order->save();
// You can also fire a custom event here, so that you can hook to it and perform any order actions
do_action( 'prefix_email_order_tracking_added', $order->get_id(), $order );
return $order;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment