Last active
September 19, 2019 12:31
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 ); | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To trigger the manual update go to Woocommerce->Orders page and add &update_tracking_manually=yes in url and press enter.