【问题标题】:Saving the value of a custom field phone number in WooCommerce My account > Account details在 WooCommerce 我的帐户 > 帐户详细信息中保存自定义字段电话号码的值
【发布时间】:2017-08-11 21:33:40
【问题描述】:

尝试将 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 显示为值的表单图像:

但在用新号码更新后它不会改变

我怎样才能让它被保存/更新?

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce user-data


    【解决方案1】:

    您需要在 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 中的完整计费电话字段代码应类似于:

    <?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 } ?>
    

    【讨论】:

    • 非常感谢。辛苦了!
    • 这也对我有用。我将它与this article 结合起来以屏蔽电话号码字段。
    猜你喜欢
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    • 1970-01-01
    • 2023-02-22
    • 2020-09-25
    • 2019-02-05
    • 2020-04-15
    • 1970-01-01
    相关资源
    最近更新 更多