通过设计,所有三个表单--Check Out、REGISTER和EDIT_ACCOUNT是相同的.所以我必须添加所有需要的字段来编辑帐户表单,因为从我的帐户编辑地址表单在设计上根本不需要.我已经添加了所有的字段,但与国家字段有一些问题.它必须与 Select 器2一起使用,一切都很好,但它不会更新. 创建新用户时,它会将国家/地区保存在用户元数据中. 但在编辑帐户页面中,我无法更改国家/地区.更新后,它仍然是注册时保存的那个.为了进行比较,我也复制了城市字段作为一个示例.
add_action( 'woocommerce_edit_account_form', 'add_billing_phone_to_edit_account_form' );
function add_billing_phone_to_edit_account_form() {
$user = wp_get_current_user();
?>
<div class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide form-group">
<label for="reg_billing_city"><?php _e( 'Town/City', 'woocommerce' ); ?></label>
<input type="text" name="billing_city" id="reg_billing_city" placeholder="<?php _e( 'Town/City', 'woocommerce' ); ?>" value="<?php echo get_user_meta( get_current_user_id(), 'billing_city', true );?>" class="woocommerce-Input woocommerce-Input--text input-text form-control" size="25" />
</div>
<?php
wp_enqueue_script('wc-country-select');
woocommerce_form_field('billing_country',array(
'type' => 'country',
'class' => array('dop'),
'label' => __('Country'),
'placeholder' => __('Choose your country.'),
'required' => true,
'clear' => true,
'default' => 'FR'
));
}
add_action( 'woocommerce_save_account_details', 'save_billing_city_account_details', 16, 1 );
function save_billing_city_account_details( $customer_id ) {
if( isset( $_POST['billing_city'] ) )
update_user_meta( $customer_id, 'billing_city', sanitize_text_field( $_POST['billing_city'] ) );
}
add_action( 'woocommerce_save_account_details', 'save_billing_country_account_details', 17, 1 );
function save_billing_country_account_details( $customer_id ) {
if( isset( $_POST['billing_country'] ) )
update_user_meta( $customer_id, 'billing_country', sanitize_text_field( $_POST['billing_country'] ) );
}
我try 使用$USER_ID而不是$CUSTOMER_ID--它没有帮助. 对于City,我使用相同的方法进行更新,而City更新得很好.如邮政编码、电话等 这个国家有什么特别之处?