0

I am having a problem adding a custom field on the checkout of WooCommerce, I am using this code:

add_filter('woocommerce_checkout_fields','custom_override_checkout_fields');
function custom_override_checkout_fields($fields) {
    $fields['billing']['billing_colonia'] = array(
        'label' => __('Colonia', 'woocommerce'),
        'placeholder' => _x('', 'placeholder', 'woocommerce'),
        'required' => true,
        'class' => array('form-row-wide'), 
        'clear' => true
    );
    
    $fields['shipping']['shipping_colonia'] = array(
        'label' => __('Colonia', 'woocommerce'),
        'placeholder' => _x('', 'placeholder', 'woocommerce'),
        'required' => true,
        'class' => array('form-row-wide'), 
        'clear' => true
    );
    return $fields;
}

It indeed shows the field on the checkout form, but I fill it and I complete an order and nothing about this new field is saved on any order at /wp-admin/edit.php?post_type=shop_order

Should I do something to save that info? What is the correct way to add a custom field there?

1 Answer 1

2

As far as I remember you need to manually save your custom fields values via woocommerce_checkout_update_order_meta hook. Try

add_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_fields_update_order_meta' );
function custom_checkout_fields_update_order_meta( $order_id ) {
    update_post_meta( $order_id, 'billing_colonia', sanitize_text_field( $_POST['billing_colonia'] ) );
    update_post_meta( $order_id, 'shipping_colonia', sanitize_text_field( $_POST['shipping_colonia'] ) );
}
1
  • Thank you so much, that was it! Oct 10, 2020 at 2:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.