1

I've added a custom field in my functions.php using the answer given in this question: Add a custom field in Woocommerce Edit Account page

My code for this is:

// Add the custom field "ukara"
add_action( 'woocommerce_edit_account_form_start', 'add_ukara_to_edit_account_form' );
add_action( 'show_user_profile', 'add_ukara_to_edit_account_form' );
function add_ukara_to_edit_account_form() {
    $user = wp_get_current_user();
    ?>
        <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
        <label for="ukara"><?php _e( 'Ukara No.', 'woocommerce' ); ?></label>
        <input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="ukara" id="ukara" value="<?php echo esc_attr( $user->ukara ); ?>" />
    </p>
    <?php
}

// Save the custom field 'ukara' 
add_action( 'woocommerce_save_account_details', 'save_ukara_account_details', 12, 1 );
function save_ukara_account_details( $user_id ) {
    // For Favorite color
    if( isset( $_POST['ukara'] ) )
        update_user_meta( $user_id, 'ukara', sanitize_text_field( $_POST['ukara'] ) );

}

How do I add that field to the users section in admin view so I can see a particular users data for that field?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

1 Answers1

1

There are some mistakes in your code. Use the following to also add/update this user custom field to Wp user pages:

// Add the custom field "ukara" to WooCommerce my account - edit account
add_action( 'woocommerce_edit_account_form_start', 'add_ukara_to_edit_account_form' );
function add_ukara_to_edit_account_form() {
    $value = get_user_meta( get_current_user_id(), 'ukara', true );
    ?>
        <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
        <label for="ukara"><?php _e( 'Ukara No.', 'woocommerce' ); ?></label>
        <input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="ukara" id="ukara" value="<?php echo $value; ?>" />
    </p>
    <?php
}

// Add the custom field "ukara" to wp user
add_action( 'show_user_profile', 'add_ukara_to_wp_user_pages' );
add_action ( 'edit_user_profile', 'add_ukara_to_wp_user_pages' );
function add_ukara_to_wp_user_pages( $user ) {
    $value = get_user_meta( $user->ID, 'ukara', true );
    ?>
        <table class="form-table"><tr>
            <th><label for="ukara"><?php _e( 'Ukara No.', 'woocommerce' ); ?></label></th>
            <td><input type="text" name="ukara" value="<?php echo esc_attr($value); ?>" class="regular-text" id="ukara" /></td>
        </tr></table><br />
    <?php
}

// Save the custom field 'ukara'
add_action( 'woocommerce_save_account_details', 'save_ukara_user_custom_field' );
add_action ( 'personal_options_update', 'save_ukara_user_custom_field' );
add_action ( 'edit_user_profile_update', 'save_ukara_user_custom_field' );
function save_ukara_user_custom_field( $user_id ) {
    if( isset( $_POST['ukara'] ) ) {
        update_user_meta( $user_id, 'ukara', sanitize_text_field( $_POST['ukara'] ) );
    }
}

Code goes in functions.php file of your active child theme (or active theme). Tested and work.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399