IN WooCommerce, I have been able to add a custom fields in Edit Account page.
I have tried adding a 2nd custom field "Favorite Color 2" but I ca't get it working, There is something that I am doing wrong.
How I can make to add/save an additional custom field in Edit Account page?
// Add the custom field "favorite_color"
add_action( 'woocommerce_edit_account_form', 'add_favorite_color_to_edit_account_form' );
function add_favorite_color_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="favorite_color"><?php _e( 'Favorite color', 'woocommerce' ); ?>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="favorite_color" id="favorite_color" value="<?php echo esc_attr( $user->favorite_color ); ?>" />
</p>
<?php
}
// Save the custom field 'favorite_color'
add_action( 'woocommerce_save_account_details', 'save_favorite_color_account_details', 12, 1 );
function save_favorite_color_account_details( $user_id ) {
// For Favorite color
if( isset( $_POST['favorite_color'] ) )
update_user_meta( $user_id, 'favorite_color', sanitize_text_field( $_POST['favorite_color'] ) );
// For Billing email (added related to your comment)
if( isset( $_POST['account_email'] ) )
update_user_meta( $user_id, 'billing_email', sanitize_text_field( $_POST['account_email'] ) );
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…