save select field on checkout page woocommerce

I’m new to WooCommerce.I’m unable to figure out where is problem here is my code

I have added a select field in billing form of checkout page.

Read More

Problem

records are not saving or updating on submitting. Problem is in Update the order meta with field value.value is not updating in database

// checkout page customization start

global $post, $woocommerce; 
// Account select field
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields($fields) {

     $fields['billing']['Account'] = array(
'type'          => 'select',
'class'         => array('billing form-row-wide'),
'label'         => __('Choose an Account'),
'placeholder'   => _x('Account', 'placeholder', 'woocommerce'),
'options'     => array(
                    '' => __( 'Select Account','' ),              
                    ),
'required'    => true,
 );
  return $fields;

}



add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
global $woocommerce;

if (!$_POST['Account'])
$woocommerce->add_error( __('Please enter your Account.'.$_POST['Account']));
}
///**

Problem area
value is not updating in database

//* 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 ($_POST['Account']) update_post_meta( $order_id, 'Account',      esc_attr($_POST['Account']));
}

/**
* Update the user meta with field value
**/
add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta');
function my_custom_checkout_field_update_user_meta( $user_id ) {
if ($user_id && $_POST['Account']) update_user_meta( $user_id, 'Account', esc_attr($_POST['Account']) );
}

Related posts

Leave a Reply

1 comment

  1. The functions look pretty accurate to me. Are you sure they aren’t updating? Or perhaps you are having difficulty is displaying them.

    I’ve tweaked them a bit. First, to use the $posted variabled that WooCommerce sends to the function, though this is trivial as $_POST should be the same. And secondly, you are using esc_attr() when you should be using sanitize_text_field(). The former is for displaying the data in an HTML attribute while the latter is for sanitizing before saving.

    //* Update the order meta with field value
    //**/
    add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta', 10, 2 );
    
    function my_custom_checkout_field_update_order_meta( $order_id, $posted ) {
        if ( isset( $posted['Account'] ) ){
            update_post_meta( $order_id, 'Account', sanitize_text_field( $posted['Account'] ) );
        }
    }
    
    /**
    * Update the user meta with field value
    **/
    add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta', 10, 2 );
    function my_custom_checkout_field_update_user_meta( $user_id, $posted ) {
        if ( $user_id && isset( $posted['Account'] ) ){
            update_user_meta( $user_id, 'Account', sanitize_text_field( $posted['Account'] ) );
        } 
    }