2017-04-12 139 views
3

我目前正試圖從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> 

不完全知道如何,我敢肯定,這正是添加到註冊頁面它有話做一個循環允許的國家和什麼不是,所以任何幫助將不勝感激!

Edit3:我找到了我的一個側面問題的答案,我將如何設置默認國家,這可以通過在woocommerce_form_field數組中添加'default'=>'XX'來完成。 XX代表國家代碼,因此對於比利時,這將是BE。

先感謝您的詳細信息。

TL; DR: enter image description here

結論:問題與HelgaTheVviking和Raunak古普塔,既幫助解決在我的整個註冊頁面,包括所有的部分(和自定義增值稅頁面目前全碼)如下:

/* ---------------------- 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'])); 
    } 
} 

我感謝大家誰在這個過程中,並與我以前的問題,幫助我的好,我的整個項目幾乎就要結束了,所以我真的要感謝#1共同體他們的大力支持!

+0

你跟WooCommerce註冊加入一個領域,而結賬或WordPress的註冊表格本身? –

+0

http://stackoverflow.com/questions/33934028/using-the-woocommerce-api-to-get-the-country-state-lists –

+1

http://stackoverflow.com/questions/34757408/wc-countries-states -selection-下拉-woocommerce –

回答

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輔助下拉。

+0

嗨,再次HelgaTheViking!感謝您的回覆,但是將類型更改爲國家/地區並不會改變外觀,恐怕我的主要帖子中的外觀仍然像圖片左側一般。這是網頁的URL,以免任何形式的幫助:https://uklederwaren.be/my-account/ –

+0

您的網址正在建設中。但我發現你需要排隊國家選擇腳本。看我的編輯。這對我來說很有用,雖然它可能不是你希望顯示該字段的地方。 – helgatheviking

+0

是的,這個網址在我發佈後只公開了大約3個小時,因爲它仍然是一個不應該公開的網站,我只能在發佈後的3小時內打開它,我想提到這個,但是我無法編輯我的帖子了。 這就是說,我確實嘗試了你的代碼片段懸停,沒有工作完全沒有任何領域出現。我也考慮過自己排列腳本,但即使在默認標題中添加腳本代碼,也沒有達到預期效果。 然而,現在接受的JS代碼片段的答案確實對我有用。不過,我還是非常感謝你的幫助! –

2

如果你看到WooCommerce結帳頁面的HTML源代碼,那麼你將看到 它使用select2 JS插件。所以,你需要傳遞一個額外的 ID /類woocommerce_form_field()然後叫JS選擇2功能 在頁腳。

所以你完整的代碼將

//to add billing_country 
function wh_extra_register_fields() 
{ 
    $countries_obj = new WC_Countries(); 
    $countries = $countries_obj->get_allowed_countries(); 
    woocommerce_form_field('billing_country', array(
     'type' => 'select', 
     'input_class' => array('myClass'), //<--check this line 
     'label' => __('Country'), 
     'placeholder' => __('Choose your country.'), 
     'options' => $countries, 
     'required' => true, 
     'clear' => true, 
     'default' => 'BE' //<--check this line 
    )); 
} 

add_action('woocommerce_register_form_start', 'wh_extra_register_fields'); 

//to add js code in My Account page footer 
function wh_AccountPageJS() 
{ 
    if (is_account_page()) 
    { 
     echo '<script>jQuery(".myClass").select2();</script>'; 
    } 
} 

add_action('wp_footer', 'wh_AccountPageJS', 100); 

代碼放在functions.php文件的活躍兒童主題(或主題)的。或者也可以在任何插件php文件中使用。
代碼已經過測試和工作。版本3.0。

enter image description here

希望這有助於!

+0

非常感謝,這對我有用!我也首先想到,它必須是像我發現的腳本那樣排隊等待的東西,但那也不起作用。但是你提供的腳本片段直接工作。所以,感謝您的幫助,並且您已經得到了獎勵並被標記爲有效;) 非常感謝您的協助。 –

+0

@RafaëlDeJongh:我在過去使用過選擇框jQuery插件,所以我知道我必須初始化它。順便說一句,我很高興它爲你工作,作爲回報,我還得學習如何定製註冊頁面。 –

+0

再一次感謝,未來對HelgaTheViking代碼的調查確實能夠奏效,並且由於它使用了WC的更多內置功能,因此我現在將接受她的回答爲有效。但是要知道,你的代碼爲我的第一個工作,而我在實現其他代碼時遇到了一些麻煩。 無論哪種方式,我非常感謝您的幫助和幫助! –