从 WooCommerce 注册中的国家/地区下拉列表中保存所选国家/地区

问题描述 投票:0回答:1

我正在努力处理 WooCommerce 的自定义注册字段。 我需要 2 个注册字段。

  1. B2B
  2. B2C

所以我想创建两个注册页面,一个用于 B2B,一个用于 B2C。

我创建了页面,添加了代码并在数据库中注册了新字段。这一切正常,但不适用于 1 个字段。这是一个用于选择国家/地区的下拉字段。

下面的代码是正常输入。该字段在注册时在数据库中注册,并且值从注册用户添加到 WooCommerce 帐户页面。

<div class="col-md-12">
    <label for="reg_billing_city"><?php _e( 'City', 'woocommerce' ); ?><span class="required">*</span></label>
    <input type="text" class="input-text" name="billing_city" id="reg_billing_city" value="<?php if ( ! empty( $_POST['billing_city'] ) ) esc_attr_e( $_POST['billing_city'] ); ?>" />
</div>

我还需要在帐户页面上注册用户的国家/地区。我正在使用下面的代码:

<div class="col-md-12">
    <label for="reg_billing_country"><?php _e( 'Country', 'woocommerce' ); ?><span class="required">*</span></label>
    <select class="input-text" name="billing_country" id="reg_billing_country" value="<?php if ( ! empty( $_POST['billing_country'] ) ) esc_attr_e( $_POST['billing_country'] ); ?>">
        <option value="Nederland">Nederland</option>
        <option value="Belgie">Belgie</option>
        <option value="Duitsland">Duitsland</option>
        <option value="Frankrijk">Frankrijk</option>
    </select>
</div>

但是 billing_country 中的值不会添加到海关帐户页面。

if ( isset( $_POST['billing_country'] ) ) {
    update_user_meta( $customer_id, 'billing_country', sanitize_text_field( $_POST['billing_country'] ) );
}

那么如何将下拉列表的值更新到用户元呢?

php woocommerce hook-woocommerce registration country-codes
1个回答
0
投票

您的帐单国家/地区字段代码存在一些错误:

  1. 您需要将国家代码设置为
    <option value="{$country_code}">
  2. value=""
    本身没有
    <select>
    ,需要为对应的选中选项值设置属性
    selected

这是完整的代码,显示字段,验证并保存:

add_filter( 'woocommerce_register_form', 'additional_registration_fields' );
function additional_registration_fields() {
    $countries = WC()->countries->get_countries(); // Get countries array (code / label name pairs)
    $selected  = isset($_POST['billing_country']) ? esc_attr($_POST['billing_country']) : '';
    ?>
    <div class="col-md-12 form-row form-row-wide">
        <label for="reg_billing_country"><?php _e( 'Country', 'woocommerce' ); ?> <span class="required">*</span></label>
        <select class="country_select input-text" name="billing_country" id="reg_billing_country"><?php 
            printf('<option value="" %s> %s</option>', selected('', $selected, false), __('Select your country','woocommerce') );
            
            foreach ( array('NE','BE','DE','FR') as $key ) :
                printf('<option value="%s" %s>%s</option>', $key, selected($key, $selected, false), $countries[$key] );
            endforeach; 
            ?>
        </select>
    </div>
    <?php
    $billing_city = isset($_POST['billing_country']) ? sanitize_text_field($_POST['billing_country']) : '';
    ?>
    <div class="col-md-12 form-row form-row-wide">
        <label for="reg_billing_city"><?php _e( 'City', 'woocommerce' ); ?><span class="required">*</span></label>
        <input type="text" class="input-text" name="billing_city" id="reg_billing_city" value="<?php echo $billing_city; ?>" />
    </div>
    <?php
}

// Validate WooCommerce registration form custom fields.
add_action( 'woocommerce_register_post', 'wc_validate_reg_form_fields', 10, 3 );
function wc_validate_reg_form_fields($username, $email, $validation_errors) {
    if (isset($_POST['billing_country']) && empty($_POST['billing_country']) ) {
        $validation_errors->add('billing_country_error', __('Country is a required field.', 'woocommerce'));
    }
    if (isset($_POST['billing_city']) && empty($_POST['billing_city']) ) {
        $validation_errors->add('billing_city_error', __('City is a required field.', 'woocommerce'));
    }
    return $validation_errors;
}

// Save WooCommerce registration custom fields values.
add_action( 'woocommerce_created_customer', 'wc_save_registration_form_fields' );
function wc_save_registration_form_fields( $customer_id ) {
    if ( isset($_POST['billing_country']) && ! empty($_POST['billing_country']) ) {
        update_user_meta( $customer_id, 'billing_country', esc_attr($_POST['billing_country']) );
    }
    if ( isset($_POST['billing_city']) && ! empty($_POST['billing_city']) ) {
        update_user_meta( $customer_id, 'billing_city', sanitize_text_field($_POST['billing_city']) );
    }
}

代码位于子主题的functions.php 文件中(或插件中)。应该可以。

© www.soinside.com 2019 - 2024. All rights reserved.