简体   繁体   English

在我的帐户上添加手机字段 > 在 Woocommerce 中编辑帐户

[英]Add a mobile phone field on My account > edit account in Woocommerce

my question: How to add mobile phone field in Woocommerce my-account/edit-account/ page (Related template: form-edit-account.php file)我的问题:如何在Woocommerce的my-account/edit-account/页面添加手机字段(相关模板: form-edit-account.php文件)

Like in the following answer thread:就像在以下答案线程中一样:
Saving the value of a custom field phone number in WooCommerce My account > Account details 在 WooCommerce 我的帐户 > 帐户详细信息中保存自定义字段电话号码的值

But this answer code is incomplete as there is some missing hooked functions.但是此答案代码不完整,因为缺少一些挂钩函数。 Any help is appreciated to get something complete, meaning the field display.感谢任何帮助来完成一些事情,这意味着现场显示。

You have 3 options to display a custom mobile phone field on My account > Edit account page:您有 3 个选项可以在我的帐户 > 编辑帐户页面上显示自定义手机字段:

1) As the first field using woocommerce_edit_account_form_start action hook (see below). 1) 作为使用woocommerce_edit_account_form_start动作钩子的第一个字段(见下文)。

2) After existing fields using woocommerce_edit_account_form action hook: 2) 在使用woocommerce_edit_account_form动作钩子的现有字段之后

// Display the mobile phone field
// add_action( 'woocommerce_edit_account_form_start', 'add_billing_mobile_phone_to_edit_account_form' ); // At start
add_action( 'woocommerce_edit_account_form', 'add_billing_mobile_phone_to_edit_account_form' ); // After existing fields
function add_billing_mobile_phone_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="billing_mobile_phone"><?php _e( 'Mobile phone', 'woocommerce' ); ?> <span class="required">*</span></label>
        <input type="text" class="woocommerce-Input woocommerce-Input--phone input-text" name="billing_mobile_phone" id="billing_mobile_phone" value="<?php echo esc_attr( $user->billing_mobile_phone ); ?>" />
    </p>
    <?php
}

// Check and validate the mobile phone
add_action( 'woocommerce_save_account_details_errors','billing_mobile_phone_field_validation', 20, 1 );
function billing_mobile_phone_field_validation( $args ){
    if ( isset($_POST['billing_mobile_phone']) && empty($_POST['billing_mobile_phone']) )
        $args->add( 'error', __( 'Please fill in your Mobile phone', 'woocommerce' ),'');
}

// Save the mobile phone value to user data
add_action( 'woocommerce_save_account_details', 'my_account_saving_billing_mobile_phone', 20, 1 );
function my_account_saving_billing_mobile_phone( $user_id ) {
    if( isset($_POST['billing_mobile_phone']) && ! empty($_POST['billing_mobile_phone']) )
        update_user_meta( $user_id, 'billing_mobile_phone', sanitize_text_field($_POST['billing_mobile_phone']) );
}

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and works.测试和工作。

3) In a specific location , overriding myaccount/form-edit-account.php template file via the theme as explained on this documentation . 3)在特定位置,通过主题覆盖myaccount/form-edit-account.php模板文件,如本文档所述 and on this answer thread这个答案线程上......

In this case you will need to add the following html code in the template ( like in this answer thread ) :在这种情况下,您需要在模板中添加以下 html 代码就像在这个答案线程中一样

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

In this last case, you will need to add in your theme's function.php file the 2 last hooked functions from section 2 (validation and saving).在最后一种情况下,您需要在主题的 function.php 文件中添加第 2 节(验证和保存)中最后两个挂钩的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将年龄字段添加到woocommerce编辑帐户 - How to add age field to woocommerce Edit account 在 Woocommerce 编辑帐户页面中添加自定义字段 - Add a custom field in Woocommerce Edit Account page 在我的帐户中添加个人资料图片(文件上传)&gt; 在 WooCommerce 中编辑帐户 - Add a profile picture (file upload) on My account > edit account in WooCommerce 在我的帐户上添加带有验证的单选按钮 &gt; 在 WooCommerce 中编辑帐户 - Add radio buttons with validation on My account > edit account in WooCommerce 仅允许在我的帐户上编辑每个客户一次自定义字段 &gt; 在 WooCommerce 中编辑帐户 - Only allow edit of custom field once per customer on My account > edit account in WooCommerce woocommerce添加占位符文本以编辑帐户表单 - woocommerce add placeholder text to edit account form 如何在 WooCommerce 结帐中编辑我的帐户部分? - How to edit my account section in WooCommerce checkout? Woocommerce - 如何使用 elementor 编辑仪表板我的帐户 - Woocommerce - How to edit dashboard my account with elementor 从WooCommerce中我的帐户编辑地址中删除计费电话和电子邮件字段 - Remove billing phone and email fields from my account edit address in WooCommerce 将用户自定义帐户字段添加到 WooCommerce 结帐 - Add user custom account field to WooCommerce checkout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM