Saving the value of a custom field phone number in WooCommerce My account > Account details

爱⌒轻易说出口 提交于 2019-12-09 13:53:21

问题


Trying to add a field for Woocommerce billing_phone to the my-account/edit-account/. It is present within the update address pages but when adding to the form-edit-account.php it does not update.

Currently adding by following the same code as the other fields, such as first_name:

<input type="text" class="form-control woocommerce-Input woocommerce-Input--phone input-text" name="billing_phone" id="billing_phone" value="<?php echo esc_attr( $user->billing_phone ); ?>" /> 

Image of form with billing_phone being shown as a value:

But upon updating with new number it is not changed

How can I make this being saved/updated?


回答1:


You need to add a custom function hooked in woocommerce_save_account_details action hook:

add_action( 'woocommerce_save_account_details', 'my_account_saving_billing_phone', 10, 1 );
function my_account_saving_billing_phone( $user_id ) {
    $billing_phone = $_POST['billing_phone'];
    if( ! empty( $billing_phone ) )
        update_user_meta( $user_id, 'billing_phone', sanitize_text_field( $billing_phone ) );
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested on WooCommerce version 3+ and works. The data will be updated and displayed correctly.


Your complete billing phone field code in form-edit-account.php should be something like:

 <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
    <label for="billing_phone"><?php _e( 'Phone', 'woocommerce' ); ?> <span class="required">*</span></label>
    <input type="text" class="woocommerce-Input woocommerce-Input--phone input-text" name="billing_phone" id="billing_phone" value="<?php echo esc_attr( $user->billing_phone ); ?>" />
</p>


来源:https://stackoverflow.com/questions/45644093/saving-the-value-of-a-custom-field-phone-number-in-woocommerce-my-account-acco

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!