$order_id issue with a WooCommerce Order

Since I upgraded from WooCommerce 2.6 to 3 I have run into a problem whereby some custom code of mine does not run when an order is processed. I have been in touch with WooCommerce’s help people without luck and I have been through many pages on Stackoverflow without seeing mention of the issue.

In my functions.php file I have this code that does an update to the database of a custom field (a website address) … and it works 100% (both in WC 2.6 & WC 3):

add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );

function my_custom_checkout_field_update_order_meta( $order_id ) {
    if ( ! empty( $_POST['website_address'] ) ) {
    update_post_meta( $order_id, 'Website Address', sanitize_text_field( $_POST['website_address'] ) );

The fact that this code works every time suggests to me that the value of $order_id must exist at this step.

But if, just before the successful code runs, I have a piece of code such as :

IF ($order_id) {
  // Email me ...

… then the email never fires.

In version 2.6 I had no issue with this and the email would fire off and include the value of $order_id.

I have even experimented with leaving out the IF statement for ‘IF ($order_id)’ so as to test if the email script is working fine and it is.

So my question is, how come I cannot access the value of $order_id yet the code immediately after, that uses $order_id, works fine?

Any suggestions / insights welcome.


Read more here: $order_id issue with a WooCommerce Order

Leave a Reply

Your email address will not be published. Required fields are marked *