Function WooCommerce-是否将国家/州选择字段添加到注册表?

Function WooCommerce-是否将国家/州选择字段添加到注册表?,function,woocommerce,Function,Woocommerce,我已经通读了以下教程,并成功地将几个自定义字段添加到“我的帐户”页面上的注册表中(用户注销时可以看到) 如果店主定义了账单国家/州,我想将其添加为选择字段。(即,如果商店仅服务于加拿大,则仅加载省份) 不幸的是,本教程没有涵盖选择字段,只涉及基本文本字段。我确实找到了另一个线程,它列出了如何添加国家,但它是所有国家,而不是店主指定的国家 有人对店主设置的装货国家/省/州有进一步的了解吗?到目前为止,我得到了以下信息: /** * Add new register fields for WooC

我已经通读了以下教程,并成功地将几个自定义字段添加到“我的帐户”页面上的注册表中(用户注销时可以看到)

如果店主定义了账单国家/州,我想将其添加为选择字段。(即,如果商店仅服务于加拿大,则仅加载省份)

不幸的是,本教程没有涵盖选择字段,只涉及基本文本字段。我确实找到了另一个线程,它列出了如何添加国家,但它是所有国家,而不是店主指定的国家

有人对店主设置的装货国家/省/州有进一步的了解吗?到目前为止,我得到了以下信息:

/**
 * Add new register fields for WooCommerce registration.
 *
 * @return string Register fields HTML.
 */
function wooc_extra_register_fields() {

    $countries_obj = new WC_Countries();
    $countries = $countries_obj->__get('countries');
?>

    <fieldset>
        <legend><h3>Address</h3></legend>
        <p>Please provide the main contact address for this account.</p>
        <p class="form-row form-row-wide">
            <label for="reg_billing_address_1"><?php _e( 'Address', 'woocommerce' ); ?> <span class="required">*</span></label>
            <input type="text" class="input-text" name="billing_address_1" id="reg_billing_address_1" placeholder="Street/P.O Box address" value="<?php if ( ! empty( $_POST['billing_address_1'] ) ) esc_attr_e( $_POST['billing_address_1'] ); ?>" />
        </p>

        <div class="clear"></div>

        <p class="form-row form-row-wide">
            <label for="reg_billing_address_2"><?php _e( 'Address Line 2', 'woocommerce' ); ?></label>
            <input type="text" class="input-text" name="billing_address_2" id="reg_billing_address_2" placeholder="Apartment, suite, unit etc. (optional)" value="<?php if ( ! empty( $_POST['billing_address_2'] ) ) esc_attr_e( $_POST['billing_address_2'] ); ?>" />
        </p>

        <div class="clear"></div>

        <p class="form-row form-row-wide">
            <label for="reg_billing_city"><?php _e( 'Town/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'] ); ?>" />
        </p>

        <div class="clear"></div>  

        <p class="form-row form-row-wide">
            <label for="reg_billing_postcode"><?php _e( 'Postal Code', 'woocommerce' ); ?></label>
            <input type="text" class="input-text" name="billing_postcode" id="reg_billing_postcode" value="<?php if ( ! empty( $_POST['billing_postcode'] ) ) esc_attr_e( $_POST['billing_postcode'] ); ?>" />
        </p>

        <div class="clear"></div>

        <p class="form-row form-row-wide">
            <label for="reg_billing_country"><?php _e( 'Country', 'woocommerce' ); ?> <span class="required">*</span></label>
            <select class="country_select" name="billing_country" id="reg_billing_country">
                <?php foreach ($countries as $key => $value): ?>
                <option value="<?php echo $key?>"><?php echo $value?></option>
                <?php endforeach; ?>
            </select>
        </p>
    </fieldset>   

    <?php
}

add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );
/**
*为电子商务注册添加新的注册字段。
*
*@返回字符串寄存器字段HTML。
*/
函数wooc_额外_寄存器_字段(){
$countries_obj=新的WC_国家();
$countries=$countries\u obj->\u get('countries');
?>
地址
请提供此帐户的主要联系地址

*


也许太晚了,但是:

而不是:

<p class="form-row form-row-wide">
        <label for="reg_billing_country"><?php _e( 'Country', 'woocommerce' ); ?> <span class="required">*</span></label>
        <select class="country_select" name="billing_country" id="reg_billing_country">
            <?php foreach ($countries as $key => $value): ?>
            <option value="<?php echo $key?>"><?php echo $value?></option>
            <?php endforeach; ?>
        </select>
    </p>

*


要以其他形式添加国家/州字段,请在模板中使用以下代码:

<?php
$field = [
        'type' => 'country',
        'label' => 'Country',
        'required' => 1,
        'class' => ['address-field']
];
woocommerce_form_field( 'billing_country', $field, '' );
$field = [
    'type' => 'state',
    'label' => 'State',
    'required' => 1,
    'class' => ['address-field'],
    'validate' => ['state']
];
woocommerce_form_field( 'billing_state', $field, '' );
?>

要将国家/地区字段选择与国家/地区字段选项联系起来,请将这些字段放入具有类woocommerce billing fields的容器中,并在页面中包含woocommerce Country-select.js和表单:

<?php
$handle = 'wc-country-select';
wp_enqueue_script($handle, get_site_url().'/wp-content/plugins/woocommerce/assets/js/frontend/country-select.min.js', array('jquery'), true);
?>


谢谢,我正在寻找运行cascade state和province字段的js部分,我用它实现了一个自定义注册表单。谢谢,我发现这个过滤器很有用,只用于获取在woocommerce中启用的内容。
<?php
$handle = 'wc-country-select';
wp_enqueue_script($handle, get_site_url().'/wp-content/plugins/woocommerce/assets/js/frontend/country-select.min.js', array('jquery'), true);
?>