首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WooCommerce -是否将国家/地区选择字段添加到注册表?

问WooCommerce -是否将国家/地区选择字段添加到注册表?
EN

Stack Overflow用户
提问于 2016-07-13 00:41:39
回答 2查看 10.4K关注 0票数 2

我已经通读并遵循了下面的教程How to add custom fields in user registration on the "My Account" page,并成功地在My Account页面上的注册表单中添加了几个自定义字段(当用户注销时可以看到)。

我想添加计费国家/州作为选择字段,如果店主已经定义了它们。(例如,如果商店仅服务于加拿大,则仅加载省份)。

不幸的是,本教程没有介绍选择字段,只介绍了基本的文本字段。我确实找到了另一个帖子,上面列出了如何添加国家,但它是所有国家,而不是店主指定的国家。

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

代码语言:javascript
复制
/**
 * 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' );
EN

回答 2

Stack Overflow用户

发布于 2017-07-19 18:50:56

要以其他形式添加Woocommerce Country/State字段,请在模板中使用以下代码:

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
<?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);
?>
票数 7
EN

Stack Overflow用户

发布于 2016-10-18 00:11:05

也许为时已晚,但是:

而不是:

代码语言:javascript
复制
<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>

地点:

代码语言:javascript
复制
<?php  
$countries_obj   = new WC_Countries();
$countries   = $countries_obj->get_allowed_countries();

woocommerce_form_field('billing_country', array(
'type'       => 'select',
'class'      => array( 'chzn-drop' ),
'label'      => __('Pays'),
'placeholder'    => __('Sélectionnez un pays'),
'options'    => $countries
)
); ?>

您将获得一个允许的国家/地区的选择下拉列表

如果您想要所有国家的列表,请将"get_allowed_countries();“替换为"__get(' countries ');”

Source

状态在这个源代码中被处理

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38334675

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档