ahm*_*dwp 2 php wordpress custom-fields woocommerce hook-woocommerce
我的问题:如何在Woocommerce my-account/edit-account/页面中添加手机字段(相关模板:form-edit-account.php文件)
就像在以下答案中一样:
在WooCommerce我的帐户>帐户详细信息中保存自定义现场电话号码的值
但是,由于缺少一些挂钩函数,因此此答案代码不完整。感谢您提供任何帮助以完成某些事项,这意味着现场显示。
您有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']) );
}
Run Code Online (Sandbox Code Playgroud)
代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试和工作。
3)在特定位置,myaccount/form-edit-account.php通过本文档中说明的主题覆盖模板文件。在这个答案线程上 ...
在这种情况下,您将需要在模板中添加以下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>
Run Code Online (Sandbox Code Playgroud)
在这最后一种情况下,您将需要在主题的function.php文件中添加第2节(验证和保存)中最后两个挂钩的函数。
| 归档时间: |
|
| 查看次数: |
2552 次 |
| 最近记录: |