Automatically Update Order Status to Complete When Shipment Tracking Number Input - WooCommerce

Viewed 250

I'm trying to figure out the best way to automatically update the order status in WooCommerce when the tracking information is inputted via the official WooCommerce Shipment Tracking plugin. I found the doc from Woo on how to automatically complete orders, but I only want this to execute when the tracking number is added. Any help would be greatly appreciated!

Below is the code from Woo:

/**
 * Auto Complete all WooCommerce orders.
 */
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
function custom_woocommerce_auto_complete_order( $order_id ) { 
    if ( ! $order_id ) {
        return;
    }

    $order = wc_get_order( $order_id );
    $order->update_status( 'completed' );
}

And here are the meta references from the plugin:

The Shipping Tracking plugin stores the tracking information in the order meta with the meta key _wc_shipment_tracking_items. It’s an array with the following structure:

  • tracking_provider — String of predefined provider
  • custom_tracking_provider — String of custom provider
  • custom_tracking_link — String of custom tracking URL tracking_number
  • String of tracking number date_shipped — Timestamp of shipment date
1 Answers

As you said you are using the "WooCommerce Shipment Tracking plugin." but in that plugin, I didn't find any filters or hooks that will help to update status when the tracking number is added. but I found that they use update_post_meta() to update the tracking code so you can use the update_postmeta action hook to update order status.

Try the below code. code will go in your active theme functions.php file.

function update_order_status_when_shipment_tracking_input( $meta_id, $object_id, $meta_key, $meta_value ){
    if( $meta_key == '_wc_shipment_tracking_items' ){
        error_log('update_order_status_when_shipment_tracking_input');
        $order = wc_get_order( $object_id );
        if( $order ){
            $order->update_status( 'completed' );
        }
    }
}
add_action( 'update_postmeta', 'update_order_status_when_shipment_tracking_input', 10, 4 );
Related