Skip to content

Instantly share code, notes, and snippets.

@xadapter
Last active September 19, 2019 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xadapter/e86aac0f4e7381c3533373ae7f6edc12 to your computer and use it in GitHub Desktop.
Save xadapter/e86aac0f4e7381c3533373ae7f6edc12 to your computer and use it in GitHub Desktop.
Snippet to update live tracking status from backend for specified number of previous days. PluginHive plugin used - WooCommerce Shipment Tracking Pro - https://www.pluginhive.com/product/woocommerce-shipment-tracking-pro/
/**
* Snippet to update live tracking status from backend for specified number of previous days. To trigger the manual update go to Woocommerce->Orders page and add &update_tracking_manually=yes in url and press enter.
* Created at : 20 July 2018
* Updated at : 20 July 2018
* PluginHive Plugins : https://www.pluginhive.com/plugins/
* Gist Link : https://gist.github.com/xadapter/e86aac0f4e7381c3533373ae7f6edc12
*/
add_action( 'init', 'ph_update_live_tracking_status', 100 );
function ph_update_live_tracking_status() {
$last_days = 2; // Last how many days order tracking need to be updated
if( ! empty($_GET['update_tracking_manually']) && is_admin() && $_GET['update_tracking_manually'] ) {
$today_date = new DateTime();
$today_date->modify("- $last_days days");
if( class_exists('WfTrackingUtil') ) {
$orders_to_update = wc_get_orders( array(
'numberposts' => - 1,
'date_after' => $today_date->format("Y-m-d")
) );
foreach( $orders_to_update as $order ) {
$order_id = $order->get_id();
$shipment_source_data = $order->get_meta('wf_wc_shipment_source');
$live_api_supported_services = array( 'ups', 'fedex', 'united-states-postal-service-usps' );
if( ! empty($shipment_source_data) && in_array( $shipment_source_data['shipping_service'], $live_api_supported_services ) ) {
$shipment_result = WfTrackingUtil::get_shipment_result( $shipment_source_data );
if ( null != $shipment_result && is_object( $shipment_result ) ) {
$shipment_result_array = WfTrackingUtil::convert_shipment_result_obj_to_array ( $shipment_result );
update_post_meta( $order_id, 'wf_wc_shipment_result', $shipment_result_array );
}
}
}
}
}
}
@xadapter
Copy link
Author

To trigger the manual update go to Woocommerce->Orders page and add &update_tracking_manually=yes in url and press enter.

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