【发布时间】:2020-07-21 14:12:39
【问题描述】:
通过下面的 sn-p,我们将帐单电话字段添加到帐户编辑详细信息。
这没有问题,只是我们希望在电子邮件和密码更改部分字段之间添加新字段。
add_action( 'woocommerce_edit_account_form_start', 'misha_add_field_edit_account_form' );
// or add_action( 'woocommerce_edit_account_form', 'misha_add_field_edit_account_form' );
function misha_add_field_edit_account_form() {
woocommerce_form_field(
'billing_phone',
array(
'type' => 'text',
'required' => true, // remember, this doesn't make the field required, just adds an "*"
'label' => 'Phone',
'description' => 'add your phone number',
),
get_user_meta( get_current_user_id(), 'billing_phone', true ) // get the data
);
}
/**
* Step 2. Save field value
*/
add_action( 'woocommerce_save_account_details', 'misha_save_account_details' );
function misha_save_account_details( $user_id ) {
update_user_meta( $user_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
}
/**
* Step 3. Make it required
*/
add_filter('woocommerce_save_account_details_required_fields', 'misha_make_field_required');
function misha_make_field_required( $required_fields ){
$required_fields['billing_phone'] = 'Phone';
return $required_fields;
}
【问题讨论】:
标签: php wordpress templates woocommerce hook-woocommerce