【问题标题】:Add a mobile phone field on My account > edit account in Woocommerce在我的帐户上添加手机字段 > 在 Woocommerce 中编辑帐户
【发布时间】:2018-12-08 18:00:44
【问题描述】:

我的问题: 如何在 Woocommercemy-account/edit-account/ 页面中添加手机字段 (相关模板:form-edit-account.php文件)

就像在下面的回答线程中一样:
Saving the value of a custom field phone number in WooCommerce My account > Account details

但此答案代码不完整,因为缺少一些挂钩函数。任何帮助都可以得到完整的东西,这意味着字段显示。

【问题讨论】:

  • 谢谢,但找不到我的答案?您的答案需要编辑 2 个文件。 1:functions.php和2.form-edit-account.php。但是functions.php需要1个代码。
  • 我终于回答了……所以这是一个完整的测试解决方案

标签: php wordpress woocommerce custom-fields hook-woocommerce


【解决方案1】:

您有 3 个选项可以在“我的帐户”>“编辑帐户”页面上显示自定义手机字段:

1) 作为第一个字段,使用 woocommerce_edit_account_form_start 动作挂钩(见下文)。

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

代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

3) 在特定位置,通过主题覆盖myaccount/form-edit-account.php 模板文件,如on this documentation 所述。并在this answer thread...

在这种情况下,您需要在模板中添加以下 html 代码(like in this answer thread)

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

在最后一种情况下,您需要在主题的 function.php 文件中添加第 2 节(验证和保存)中的最后两个挂钩函数。

【讨论】:

  • 谢谢。但此代码适用于自定义字段。我需要添加手机(默认 woocommerce)> billing_phone 标准字段显示在编辑帐户页面。
  • 您只需将代码中的元密钥从billing_mobile_phone 更改为billing_phone... 现在,如果您想在任何地方处理计费电话,从我的帐户字段到结帐字段,那就是这样不同,可以用不同的方式处理。
  • 对不起,我的英语不好;我需要更改为手机(默认 woocommerce)> billing_phone 原始字段 - 将帐户页面从可写和可更改为只读
  • 在 woocommerce 编辑帐户页面中,存在的字段包括:电子邮件、名字、姓氏、显示名称和密码。我想添加 billing_phone 或任何计费字段,如邮政编码、公司(结帐字段)添加到此处
  • 在我的店里。客户在结账时注册被禁用。并需要登录并完成付款。客户在购买前使用注册页面(使用gravityforms插件创建)注册客户在帐户页面中看到帐单名称和姓氏以及电子邮件和显示名称作为默认的woocommerce设置。我希望客户在编辑帐户页面中也看到 billing_phone。比如姓名、姓氏和电子邮件。
猜你喜欢
  • 1970-01-01
  • 2018-05-15
  • 1970-01-01
  • 1970-01-01
  • 2020-09-12
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 2022-01-03
相关资源
最近更新 更多