Php 在我的帐户上添加移动电话字段>;在Woocommerce中编辑帐户
我的问题: 如何在WoocommercePhp 在我的帐户上添加移动电话字段>;在Woocommerce中编辑帐户,php,wordpress,woocommerce,custom-fields,hook-woocommerce,Php,Wordpress,Woocommerce,Custom Fields,Hook Woocommerce,我的问题: 如何在Woocommerce我的帐户/编辑帐户/页面中添加手机字段 (相关模板:表单编辑account.php文件) 类似于下面的回答线程: 但是这个答案代码是不完整的,因为缺少一些钩子函数。如需获得完整信息,请提供任何帮助,即字段显示。您有3个选项可在“我的帐户>编辑帐户”页面上显示自定义手机字段: 1)作为第一个字段使用woocmerce\u edit\u account\u form\u startaction钩子(见下文) 2)在现有字段之后使用woocommerce\u
我的帐户/编辑帐户/
页面中添加手机字段
(相关模板:表单编辑account.php
文件)
类似于下面的回答线程:但是这个答案代码是不完整的,因为缺少一些钩子函数。如需获得完整信息,请提供任何帮助,即字段显示。您有3个选项可在“我的帐户>编辑帐户”页面上显示自定义手机字段: 1)作为第一个字段使用
woocmerce\u edit\u account\u form\u start
action钩子(见下文)
2)在现有字段之后使用woocommerce\u edit\u account\u表单
操作挂钩:
// 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文件中添加第2节(验证和保存)中最后两个钩住的函数
谢谢,但是找不到我的答案吗?你的答案需要编辑2个文件。1:functions.php和2。form-edit-account.php。但是functions.php需要1个代码。我终于回答了…这是一个经过测试的完整解决方案。谢谢。但此代码适用于自定义字段。我需要在编辑帐户页面中添加移动电话(默认woocommerce)>计费电话标准字段显示。您只需将meta键从代码中的
billing\u mobile\u phone
更改为billing\u phone
。。现在,如果您想在任何地方处理计费电话,从我的帐户字段到结帐字段,对不起,我的英语不好;我需要更改为手机(默认woocommerce)>账单\手机原始字段-编辑帐户页,从可写和可更改为只读在woocommerce编辑帐户页中,存在的字段包括:电子邮件、名字、姓氏、显示名称和密码。我想在我的店铺中添加账单\电话或任何账单字段,如邮政编码、公司(结帐字段)。已禁用客户在签出期间注册。并需要登录和完成付款。客户在购买前使用注册页面(使用gravityforms插件创建)注册客户在帐户页面中查看帐单名称和姓氏以及电子邮件和显示名称作为默认设置。我希望客户也能在“编辑帐户”页面中查看账单和电话。比如名字、姓氏和电子邮件。
<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>