我已经设法在 woocommerce 注册表中添加了几个额外的字段。但是在 chechout 表单中,我添加的所有字段都不会自动填充。未出现在结帐中的字段是:“国家”和“州”。 正如您在此图像 (https://postimg.cc/vc57qkkF) 中看到的,国家和州字段出现了一个选择。但我需要显示我在注册时输入的国家和州。 这是我添加到functions.php的代码:
/**
* Add new register fields for WooCommerce registration
* To add WooCommerce registration form custom fields.
*/
add_action( 'woocommerce_register_form', 'misha_add_register_form_field' );
function misha_add_register_form_field(){
woocommerce_form_field(
'billing_first_name',
array(
'type' => 'text',
'required' => true, // just adds an "*"
'label' => 'First name'
),
( isset($_POST['billing_first_name']) ? $_POST['billing_first_name'] : '' )
);
woocommerce_form_field(
'billing_last_name',
array(
'type' => 'text',
'required' => true, // just adds an "*"
'label' => 'Last name'
),
( isset($_POST['billing_last_name']) ? $_POST['billing_last_name'] : '' )
);
woocommerce_form_field(
'billing_phone',
array(
'type' => 'tel',
'required' => true, // just adds an "*"
'label' => 'Phone'
),
( isset($_POST['billing_phone']) ? $_POST['billing_phone'] : '' )
);
woocommerce_form_field(
'billing_address_1',
array(
'type' => 'text',
'required' => true, // just adds an "*"
'label' => 'Address'
),
( isset($_POST['billing_address_1']) ? $_POST['billing_address_1'] : '' )
);
woocommerce_form_field(
'billing_city',
array(
'type' => 'text',
'required' => true, // just adds an "*"
'label' => 'City'
),
( isset($_POST['billing_city']) ? $_POST['billing_city'] : '' )
);
woocommerce_form_field(
'billing_postcode',
array(
'type' => 'text',
'required' => true, // just adds an "*"
'label' => 'Post Code'
),
( isset($_POST['billing_postcode']) ? $_POST['billing_postcode'] : '' )
);
woocommerce_form_field(
'billing_country',
array(
'type' => 'text',
'required' => true, // just adds an "*"
'label' => 'Country'
),
( isset($_POST['billing_country']) ? $_POST['billing_country'] : '' )
);
woocommerce_form_field(
'billing_state',
array(
'type' => 'text',
'required' => true, // just adds an "*"
'label' => 'State'
),
( isset($_POST['billing_state']) ? $_POST['billing_state'] : '' )
);
}
/**
* To validate WooCommerce registration form custom fields.
*/
add_action( 'woocommerce_register_post', 'misha_validate_fields', 10, 3 );
function misha_validate_fields( $username, $email, $errors ) {
if ( empty( $_POST['billing_first_name'] ) ) {
$errors->add( 'billing_first_name_error', 'First name is required!' );
}
if ( empty( $_POST['billing_last_name'] ) ) {
$errors->add( 'billing_last_name_error', 'Last name is required!' );
}
if ( empty( $_POST['billing_phone'] ) ) {
$errors->add( 'billing_phone_error', 'Phone is required!' );
}
if ( empty( $_POST['billing_address_1'] ) ) {
$errors->add( 'billing_address_1_error', 'Address is required!' );
}
if ( empty( $_POST['billing_city'] ) ) {
$errors->add( 'billing_city_error', 'City is required!' );
}
if ( empty( $_POST['billing_postcode'] ) ) {
$errors->add( 'billing_postcode_error', 'Post code is required!' );
}
if ( empty( $_POST['billing_country'] ) ) {
$errors->add( 'billing_country_error', 'Country is required!' );
}
if ( empty( $_POST['billing_state'] ) ) {
$errors->add( 'billing_state_error', 'State is required!' );
}
}
/**
* To save WooCommerce registration form custom fields.
*/
add_action( 'woocommerce_created_customer', 'misha_save_register_fields' );
function misha_save_register_fields( $customer_id ){
//First name field
if ( isset( $_POST['billing_first_name'] ) ) {
//update_user_meta( $customer_id, 'country_to_visit', wc_clean( $_POST['country_to_visit'] ) );
update_user_meta($customer_id, 'first_name', sanitize_text_field($_POST['billing_first_name']));
update_user_meta($customer_id, 'billing_first_name', sanitize_text_field($_POST['billing_first_name']));
}
//Last name field
if (isset($_POST['billing_last_name'])) {
update_user_meta($customer_id, 'last_name', sanitize_text_field($_POST['billing_last_name']));
update_user_meta($customer_id, 'billing_last_name', sanitize_text_field($_POST['billing_last_name']));
}
// WooCommerce billing phone
if ( isset( $_POST['billing_phone'] ) ) {
update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
}
// WooCommerce billing address
if ( isset( $_POST['billing_address_1'] ) ) {
update_user_meta( $customer_id, 'billing_address_1', sanitize_text_field( $_POST['billing_address_1'] ) );
}
// WooCommerce billing city
if ( isset( $_POST['billing_city'] ) ) {
update_user_meta( $customer_id, 'billing_city', sanitize_text_field( $_POST['billing_city'] ) );
}
if ( isset( $_POST['billing_postcode'] ) ) {
update_user_meta( $customer_id, 'billing_postcode', sanitize_text_field( $_POST['billing_postcode'] ) );
}
if ( isset( $_POST['billing_country'] ) ) {
update_user_meta( $customer_id, 'billing_country', sanitize_text_field( $_POST['billing_country'] ) );
}
if ( isset( $_POST['billing_state'] ) ) {
update_user_meta( $customer_id, 'billing_state', sanitize_text_field( $_POST['billing_state'] ) );
}
}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );
答案 0 :(得分:1)
我还不能添加评论,所以我在这里添加了一个问题,并尽我最大的努力同时回答......如果需要,我会编辑。
首先, 只是为了调试目的以确保我走在正确的轨道上,我猜这些字段也不会自动填充在“我的帐户”页面上,对吗?
我看到您正在为国家和州创建文本字段。您需要使用内置的 WooCommerce 的 Country 和 State 字段类型,以便将存储的值映射到选择选项,从而自动选择正确的选项。
试试这个:
misha_add_register_form_field
函数顶部,以从 WC 获取国家/地区列表。$countries_obj = new WC_Countries();
$countries = $countries_obj->__get('countries');
misha_add_register_form_field()
函数中的 country 和 state 字段替换为以下内容:woocommerce_form_field(
'billing_country',
array(
'type' => 'country',
'label' => 'Country',
'required' => 1,
'class' => ['address-field']
),
''
);
woocommerce_form_field(
'billing_state',
array(
'type' => 'state',
'label' => 'State',
'required' => 1,
'class' => ['address-field'],
'validate' => ['state']
),
''
);
您需要更新这 2 个字段的验证和清理程序,并且可能需要做一些额外的工作以确保正确更新。 您可以参考 Using the WooCommerce API to get the country/state lists 和 Add Billing Address to Woocommerce Registration Page,因为它们似乎解决了相同的挑战。