#checkout #fields #meta #order
Provides an instant overview of your shop's current customers and the contents of their carts.
A lot changed in WooCommerce 3.0! One of the changes is that the woocommerce_checkout_update_order_meta
action hook is deprecated. This hook was often used to save custom checkout field values to the order’s meta data. Here’s how to do it post 3.0 for anyone stuck in the same boat.
add_action( 'woocommerce_checkout_create_order', function( $order, $data ) {
$custom_fields = array(
'my-custom-field-1',
'my-custom-field-2',
);
foreach ( $custom_fields as $field_name ) {
if ( isset( $data[ $field_name ] ) ) {
$meta_key = '_' . $field_name;
$field_value = $data[ $field_name ];
$order->update_meta_data( $meta_key, $field_value );
}
}
}, 10, 2 );
This should be pretty self explanatory. The new action hook is woocommerce_checkout_create_order
, $custom_fields
is an array of the custom field names that you registered as billing/shipping form fields (how to do this is another topic), and their values are saved as Order meta data with their keys prepended by a _
.
Hope this helps!
Posted by Berend on
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
Hi. I tried the woocommerce_checkout_update_order_meta hook. The woocommerce_checkout_update_user_meta hook worked for me. See my code below.
<?php
function davletov_save_company_fields( $user_id ) {
$custom_fields = array('your_custom_field1','your_custom_field2');
foreach ($custom_fields as $field) {
if ( $user_id && isset($_POST[$field]) ) {
update_user_meta( $user_id, $field, sanitize_text_field($_POST[$field]) );
}
}
}
add_action( 'woocommerce_checkout_update_user_meta', 'davletov_save_company_fields', 10, 2 );