Adding Customer Field to Checkout page in WordPress

Question

I want to add extra custom field in order to get extra information from the customers. To get this job done I am using a snippet of PHP code. The field is adding but not saving the user metadata to database. 

The implemented code is:

function reigel_woocommerce_checkout_fields( $checkout_fields = array() ) {

    $checkout_fields['order']['parthner_code'] = array(
        'type'          => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'label'         => __('parthner_code'),
        'placeholder'   => __('XXX'),
        'required'      => false, 
        );

    return $checkout_fields;
}
add_filter( 'woocommerce_checkout_fields', 'reigel_woocommerce_checkout_fields' );

function reigel_woocommerce_checkout_update_user_meta( $customer_id, $posted ) {
    if (isset($posted['parthner_code'])) {


        $dob = sanitize_text_field( $posted['parthner_code'] );
        update_user_meta( $customer_id, 'parthner_code', $dob);


    }
}
add_action( 'woocommerce_checkout_update_user_meta', 'reigel_woocommerce_checkout_update_user_meta', 10, 2 );
Adding Customer Field to Checkout page in WordPress
5 (100%) 1 vote

0
Steve-baker 6 months 0 Answers 131 views Novice 0

Leave an answer

About Steve-bakerNovice