woocommerce_checkout_update_order_meta action is not working
I have just change a little bit your last hooked function and it works (on WC version 2.6.x and 3.0+). It's better with empty()
php function to use variables (to be retro compatible).
Also is better to use update_post_meta()
instead of add_post_meta()
as this function will make sure that the meta_key
already exists and if not, add_post_meta()
will be called instead...
Here a screenshot of the
wp_postmeta
table related to the order meta data:
If the meta_key
don't start by an underscore like here, it appears in backend order edit page in the Custom fields metabox:
Here is this code:
add_action( 'woocommerce_checkout_update_order_meta', 'saving_checkout_cf_data');function saving_checkout_cf_data( $order_id ) { $recipient_address = $_POST['recipient_address']; if ( ! empty( $recipient_address ) ) update_post_meta( $order_id, 'recipient_address', sanitize_text_field( $recipient_address ) ); $recipient_phone_number = $_POST['recipient_phone_number']; if ( ! empty( $recipient_phone_number ) ) update_post_meta($order_id, 'recipient_phone_number', sanitize_text_field( $recipient_phone_number ) );}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
If you want to have a
meta_key
starting by_billing…
like classic billing checkout fields, you just need to change that inupdate_post_meta()
function. For example:
update_post_meta( $order_id, '_billing_recipient_address', sanitize_text_field( $recipient_address ) );
But in this case, this will not appear in the custom Fields metabox in the order edit page.