在我的帐户上添加移动 phone 字段 > 在 Woocommerce 中编辑帐户
Add a mobile phone field on My account > edit account in Woocommerce
我的问题:
如何在 Woocommerce my-account/edit-account/
页面中添加移动 phone 字段
(相关模板:form-edit-account.php
文件)
喜欢下面的回答线程:
但是这个答案代码不完整,因为缺少一些挂钩函数。感谢任何帮助,以获得完整的东西,这意味着字段显示。
您有 3 个选项可以在“我的帐户”>“编辑帐户”页面上显示自定义手机 phone 字段:
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. and on …
在这种情况下,您需要在模板中添加以下 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).
我的问题:
如何在 Woocommerce my-account/edit-account/
页面中添加移动 phone 字段
(相关模板:form-edit-account.php
文件)
喜欢下面的回答线程:
但是这个答案代码不完整,因为缺少一些挂钩函数。感谢任何帮助,以获得完整的东西,这意味着字段显示。
您有 3 个选项可以在“我的帐户”>“编辑帐户”页面上显示自定义手机 phone 字段:
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. and on
在这种情况下,您需要在模板中添加以下 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).