Instantly share code, notes, and snippets.

Embed
What would you like to do?
WC customising checkout fields using actions and filters - Lesson 3 snippet 3
/**
* Update the order meta with field value
*/
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['my_field_name'] ) ) {
update_post_meta( $order_id, 'My Field', sanitize_text_field( $_POST['my_field_name'] ) );
}
}
@helgatheviking

This comment has been minimized.

Show comment
Hide comment
@helgatheviking

helgatheviking Sep 16, 2017

Suggestion for WC3.0+

/**
 * Update the order meta with field value
 */
add_action( 'woocommerce_checkout_create_order', 'my_custom_checkout_field_update_order_meta' );

function my_custom_checkout_field_update_order_meta( $order ) {
    if ( ! empty( $_POST['my_field_name'] ) ) {
        $order->update_meta_data( '_my_field', sanitize_text_field( $_POST['my_field_name'] ) );
    }
}

helgatheviking commented Sep 16, 2017

Suggestion for WC3.0+

/**
 * Update the order meta with field value
 */
add_action( 'woocommerce_checkout_create_order', 'my_custom_checkout_field_update_order_meta' );

function my_custom_checkout_field_update_order_meta( $order ) {
    if ( ! empty( $_POST['my_field_name'] ) ) {
        $order->update_meta_data( '_my_field', sanitize_text_field( $_POST['my_field_name'] ) );
    }
}
@robertox85

This comment has been minimized.

Show comment
Hide comment
@robertox85

robertox85 Feb 4, 2018

I noticed that the fields are saved even without this code. Is there something I'm doing wrong?

robertox85 commented Feb 4, 2018

I noticed that the fields are saved even without this code. Is there something I'm doing wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment