sam707
sam707

Reputation: 85

How to get updated WooCommerce order data?

How can i get access to updated order data in this function? I am super confused.

I found this code here.

add_action ('save_post_shop_order',
function (int $postId, \WP_Post $post, bool $update): void{
    // Ignore order (post) creation
    if ($update !== true) {
        return;
    }

    // Here comes your code...
},10,3);

Thanks in advance.

Upvotes: 2

Views: 1368

Answers (1)

Vincenzo Di Gaetano
Vincenzo Di Gaetano

Reputation: 4110

WORDPRESS HOOK

If you want to run the function you can use the edit_post_{$post->post_type} hook which is triggered only when a post is updated.

// run a function when the WooCommerce order is updated
add_action( 'edit_post_shop_order', 'edit_post_shop_order_callback', 99, 2 );
function edit_post_shop_order_callback( $post_ID, $post ) {

    // gets the WC_Order object
    $order = wc_get_order( $post_ID );

    // get the data you want
    $order_status = $order->get_status();
    $billing_first_name = $order->get_billing_first_name();
    // ...

}

The code has been tested and works. Add it to your active theme's functions.php.

WOOCOMMERCE HOOK

Otherwise you could use the woocommerce_update_order WooCommerce hook.

// run a function when the WooCommerce order is updated
add_action( 'woocommerce_update_order', 'woocommerce_update_order_callback', 99, 1 );
function woocommerce_update_order_callback( $order_id ) {
    
    // gets the WC_Order object
    $order = wc_get_order( $order_id );

    // get the data you want
    $order_status = $order->get_status();
    $billing_first_name = $order->get_billing_first_name();
    // ...

}

The code has been tested and works. Add it to your active theme's functions.php.

RELATED ANSWERS

Upvotes: 2

Related Questions