Ryan Lund
Ryan Lund

Reputation: 128

Customer ID is 0 in woocommerce_process_shop_order_meta hook

When creating a Woocommerce order using the "Add Order" screen in the Wordpress admin, if I retrieve the order using wc_get_order() in a callback attached to woocommerce_process_shop_order_meta, I have noticed that the Customer ID is 0 - despite the fact that I have selected a customer. The code I am writing uses this data to sync to an external API so my question is: Why is this ID empty and is there a way to get the correct ID?

A simplified version of the code would be:

add_action('woocommerce_process_shop_order_meta', 'processOrder', 10, 1);

function processOrder($order_id){
    $order = wc_get_order($order_id);
    die(print_r($order->get_customer_id(),1));
}

Upvotes: 2

Views: 765

Answers (1)

Reigel Gallarde
Reigel Gallarde

Reputation: 65264

It's because of the priority you're using. See attached image: enter image description here

The customer id is saved in add_action( 'woocommerce_process_shop_order_meta', 'WC_Meta_Box_Order_Data::save', 40, 2 ); where priority is 40. Setting your priority greater than 40 then it should work.

Upvotes: 1

Related Questions