注册时WooCommerce国家选择

时间:2017-04-12 15:00:25

标签: javascript php html wordpress woocommerce

我目前正在尝试将WooCommerce中的默认国家/地区搜索者添加到注册页面,而使用以下代码段可以使用默认的国家/地区列表:

<?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'     => __('Country'),
'placeholder' => __('Choose your country.'),
'options'   => $countries,
'required'  => true,
'clear'     => true
));
?>

它当然与WooCommerce的原始国家/地区选择不一样,所以我想知道是否有人知道将此国家选择字段添加到注册页面的正确方法。

编辑:如果我理解正确,你需要加载一个JavaScript库以便下拉列表正常工作,如何有人将这个加载到注册页面上以及如何设置默认国家/例如“比利时” ?

Edit2:似乎它与国家选择器的这部分脚本有关:

<script type='text/javascript' src='//uklederwaren.be/wp-content/plugins/woocommerce/assets/js/frontend/country-select.min.js?ver=2.6.14'></script>

不完全确定如何将其准确添加到注册页面,因为我确定它与允许的国家/地区的循环有关,所以任何帮助都将不胜感激!

编辑3:我找到了一个方面问题的答案,我可以设置默认国家/地区,这可以通过添加'default'=&gt;来完成。 'XX',在woocommerce_form_field数组中。 XX代表国家代码,因此对于比利时来说,这将是BE。

提前感谢您提供更多信息。

TL; DR: enter image description here

结论:在HelgaTheVviking和Raunak Gupta的帮助下解决了这个问题,我现在的整个注册页面的完整代码包括所有部分(以及自定义增值页面)如下:

/* ---------------------- Registration page ----------------------- */
//Add extra fields in registration form
add_action('woocommerce_register_form_start','my_extra_register_fields');
function my_extra_register_fields(){
?>
    <p class="woocommerce-FormRow form-row form-row-first">
        <label for="reg_billing_first_name"><?php _e('First Name','woocommerce'); ?><span class="required">*</span></label>
        <input type="text" class="input-text" name="billing_first_name" id="reg_billing_first_name" value="<?php if(! empty($_POST['billing_first_name'])) esc_attr_e($_POST['billing_first_name']); ?>"/>
    </p>
    <p class="woocommerce-FormRow form-row form-row-last">
        <label for="reg_billing_last_name"><?php _e('Last Name','woocommerce'); ?><span class="required">*</span></label>
        <input type="text" class="input-text" name="billing_last_name" id="reg_billing_last_name" value="<?php if(! empty($_POST['billing_last_name'])) esc_attr_e($_POST['billing_last_name']); ?>"/>
    </p>
    <div class="clearfix"></div>
    <p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide">
        <label for="reg_billing_company"><?php _e('Company Name','woocommerce'); ?><span class="required">*</span></label>
        <input type="text" class="input-text" name="billing_company" id="reg_billing_company" value="<?php if(! empty($_POST['billing_company'])) esc_attr_e($_POST['billing_company']); ?>"/>
    </p>
    <div class="clearfix"></div>
    <p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide">
        <label for="reg_billing_vat"><?php _e('VAT Number','woocommerce'); ?><span class="required">*</span></label>
        <input type="text" class="input-text" name="billing_vat" id="reg_billing_vat" value="<?php if(! empty($_POST['billing_vat'])) esc_attr_e($_POST['billing_vat']); ?>" maxlength="15" placeholder="Enter VAT Number"/>
    </p>
    <div class="clearfix"></div>
<?php
    wp_enqueue_script('wc-country-select');
    woocommerce_form_field('billing_country',array(
        'type'        => 'country',
        'class'       => array('chzn-drop'),
        'label'       => __('Country'),
        'placeholder' => __('Choose your country.'),
        'required'    => true,
        'clear'       => true,
        'default'     => 'BE'
    ));
?>
    <p class="woocommerce-FormRow form-row form-row-first">
        <label for="reg_billing_postcode"><?php _e('Postcode / ZIP','woocommerce'); ?><span class="required">*</span></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>
    <p class="woocommerce-FormRow form-row form-row-last">
        <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="clearfix"></div>
    <p class="woocommerce-FormRow woocommerce-FormRow--wide 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" value="<?php if(! empty($_POST['billing_address_1'])) esc_attr_e($_POST['billing_address_1']); ?>" placeholder="Street address"/>
    </p>
    <p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide">
        <input type="text" class="input-text" name="billing_address_2" id="reg_billing_address_2" value="<?php if(! empty($_POST['billing_address_2'])) esc_attr_e($_POST['billing_address_2']); ?>" placeholder="Apartment,suite,unit etc. (optional)"/>
    </p>
    <div class="clearfix"></div>
    <p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide">
        <label for="reg_billing_phone"><?php _e('Phone','woocommerce'); ?><span class="required">*</span></label>
        <input type="text" class="input-text" name="billing_phone" id="reg_billing_phone" value="<?php if(! empty($_POST['billing_phone'])) esc_attr_e($_POST['billing_phone']); ?>"/>
    </p>
    <div class="clearfix"></div>
<?php
}
//Registration form fields Validation
add_action('woocommerce_register_post','my_validate_extra_register_fields',10,3);
function my_validate_extra_register_fields($username,$email,$validation_errors){
    if(isset($_POST['billing_first_name']) && empty($_POST['billing_first_name'])){$validation_errors->add('billing_first_name_error',__('A first name is required!','woocommerce'));}
    if(isset($_POST['billing_last_name']) && empty($_POST['billing_last_name'])){$validation_errors->add('billing_last_name_error',__('A last name is required!','woocommerce'));}
    if(isset($_POST['billing_company']) && empty($_POST['billing_company'])){$validation_errors->add('billing_company_error',__('A Company name is required!','woocommerce'));}
    if(isset($_POST['billing_vat']) && empty($_POST['billing_vat'])){$validation_errors->add('billing_vat_error',__('VAT number is required!','woocommerce'));}
    if(isset($_POST['billing_country']) && empty($_POST['billing_country'])){$validation_errors->add('billing_country_error',__('A country is required!','woocommerce'));}
    if(isset($_POST['billing_city']) && empty($_POST['billing_city'])){$validation_errors->add('billing_city_error',__('A city is required!','woocommerce'));}
    if(isset($_POST['billing_postcode']) && empty($_POST['billing_postcode'])){$validation_errors->add('billing_postcode_error',__('A postcode is required!','woocommerce'));}
    if(isset($_POST['billing_state']) && empty($_POST['billing_state'])){$validation_errors->add('billing_state_error',__('A state is required!','woocommerce'));}
    if(isset($_POST['billing_address_1']) && empty($_POST['billing_address_1'])){$validation_errors->add('billing_address_1_error',__('An address is required!','woocommerce'));}
    if(isset($_POST['billing_phone']) && empty($_POST['billing_phone'])){$validation_errors->add('billing_phone_error',__('A phone number is required!','woocommerce'));}
    return $validation_errors;
}
//Below code save extra fields when new user register
add_action('woocommerce_created_customer','my_save_extra_register_fields'); 
function my_save_extra_register_fields($customer_id){
    if(isset($_POST['billing_first_name'])){
        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']));
    }
    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']));
    }
    if(isset($_POST['billing_company'])){
        update_user_meta($customer_id,'billing_company',sanitize_text_field($_POST['billing_company']));
    }
    if(isset($_POST['billing_vat'])){
        update_user_meta($customer_id,'billing_vat',sanitize_text_field($_POST['billing_vat']));
    }
    if(isset($_POST['billing_country'])){
        update_user_meta($customer_id,'billing_country',sanitize_text_field($_POST['billing_country']));
    }
    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_state'])){
        update_user_meta($customer_id,'billing_state',sanitize_text_field($_POST['billing_state']));
    }
    if(isset($_POST['billing_address_1'])){
        update_user_meta($customer_id,'billing_address_1',sanitize_text_field($_POST['billing_address_1']));
    }
    if(isset($_POST['billing_phone'])){
        update_user_meta($customer_id,'billing_phone',sanitize_text_field($_POST['billing_phone']));
    }
    if(isset($_POST['email'])){
        update_user_meta($customer_id,'billing_email',sanitize_text_field($_POST['email']));
    }
}

我感谢所有在此过程中帮助过我的人以及之前的问题,我的整个项目即将结束,所以我真的要感谢Stackoverflow社区的大力支持!

3 个答案:

答案 0 :(得分:2)

woocommerce_form_field()函数支持country类型,因此我认为这可行:

/**
 * Add new register fields for WooCommerce registration.
 */
function wooc_extra_register_fields() {

    wp_enqueue_script( 'wc-country-select' );

    woocommerce_form_field( 'billing_country', array(
        'type'      => 'country',
        'class'     => array('chzn-drop'),
        'label'     => __('Country'),
        'placeholder' => __('Choose your country.'),
        'required'  => true,
        'clear'     => true
    ));

}
add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );

修改您需要将国家/地区选择脚本排入队列以获取JS辅助下拉列表。

答案 1 :(得分:2)

  

如果您看到WooCommerce Checkout页面的源HTML,那么您将会   看它使用select2 JS插件。所以你需要额外通过   woocommerce_form_field()中的ID /类,然后调用select2 JS   功能,页脚。

所以你的完整代码将是

import grequests
from bs4 import BeautifulSoup

pages = []
page="https://www.spitogatos.gr/search/results/residential/sale/r100/m100m101m102m103m104m105m106m107m108m109m110m150m151m152m153m154m155m156m157m158m159m160m161m162m163m164m165m166m167m168m169m170m171m172m173m174m175m176m177m178m179m180m181m182m183m184m185m186m187m188m189m190m191m192m193m194m195m196m197m198m106001m125000m"
for i in range(1,1000):
    pages.append(page)
    page="https://www.spitogatos.gr/search/results/residential/sale/r100/m100m101m102m103m104m105m106m107m108m109m110m150m151m152m153m154m155m156m157m158m159m160m161m162m163m164m165m166m167m168m169m170m171m172m173m174m175m176m177m178m179m180m181m182m183m184m185m186m187m188m189m190m191m192m193m194m195m196m197m198m106001m125000m"
    page = page + "/offset_{}".format(i*10)

rs = (grequests.get(item) for item in pages)
a=grequests.map(rs)

代码进入您的活动子主题(或主题)的functions.php文件。或者也可以在任何插件php文件中。
代码经过测试并正常运行。 3.0版。

enter image description here

希望这有帮助!

答案 2 :(得分:0)

在模板/功能中添加它:

<?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 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);
?>