在 WooCommerce 我的帐户 > 帐户详细信息中保存自定义字段 phone 数字的值

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

正在尝试将 Woocommerce billing_phone 的字段添加到 my-account/edit-account/。它存在于更新地址页面中,但是当添加到 form-edit-account.php 时它不会更新。

目前正在按照与其他字段相同的代码添加,例如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 ); ?>" /> 

billing_phone 的表格图像显示为值:

但是用新号码更新后没有改变

我怎样才能让这个存在saved/updated?

您需要在 woocommerce_save_account_details 操作挂钩中添加自定义函数:

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 ) );
}

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。

此代码已在 WooCommerce 版本 3+ 上测试并有效。数据将更新并正确显示。


您在 form-edit-account.php 中的完整帐单 phone 字段代码应该类似于:

<?php function field_phone(){
$user = wp_get_current_user(); 
 ?>
 <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> <?php } ?>