2

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));
}

Reigel Gallarde
  • 64,198
  • 21
  • 121
  • 139
Ryan Lund
  • 128
  • 1
  • 9
  • have you tried testing User ID from the Order ID using user_id $user_id = $order->get_user_id(); to see if this gets an id – Brad Holmes Jan 13 '20 at 15:52
  • Hi Brad, yeah I was/am actually using get_user_id in my real-life code, I just modified it slightly here to get the point across a little better. It's probably important to note that the user/customer ids are set correctly when an order is updated - it's only missing with a new order. This makes me believe that I'm probably using the wrong hook....but the documentation for the hooks on woocommerce is not really helpful for finding the right one. – Ryan Lund Jan 13 '20 at 15:58
  • By the way, I did also print_r the entire order object and can see that it's set to 0 in general, so it's not just one of the functions being a pain. – Ryan Lund Jan 13 '20 at 16:00
  • actually look here https://stackoverflow.com/questions/22843504/how-to-get-customer-details-from-order-in-woocommerce/57562904#57562904 – Brad Holmes Jan 13 '20 at 16:06
  • Thanks man, but unfortunately that didn't help. I should also probably point out that both the billing and shipping details are also empty at this point...which again makes me believe that I'm using the wrong hook (as it looks like the customer data is saved after my code is run). – Ryan Lund Jan 13 '20 at 16:18
  • can you share your full code if possible ill have a look for you – Brad Holmes Jan 13 '20 at 16:24

1 Answers1

1

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.

Reigel Gallarde
  • 64,198
  • 21
  • 121
  • 139