2016-07-12 19 views
2

私は読んで、次のチュートリアルHow to add custom fields in user registration on the "My Account" pageに続き、マイアカウントページ(ユーザーがログアウトしたときに表示される)の登録フォームにいくつかのカスタムフィールドを追加しました。WooCommerce - 国/州を追加して登録フォームにフィールドを追加しますか?

お店のオーナーが定義している場合は、選択欄として請求先の国/州を追加したいと思います。 (つまり、カナダでのみサービスを提供する場合は、州のみをご利用ください)。

残念ながら、このチュートリアルでは、選択フィールド、基本的なテキストフィールドについては説明しません。国を追加する方法をリストした別のスレッドが見つかりましたが、それはすべての国であり、店主が指定したものではありません。

誰かが、店主が設定した国/地域/州を読み込む際にさらに詳しい情報を持っていますか?これまでのところ私は、次のを持っている:

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

答えて

2

は、多分手遅れだが:

の代わりに:

<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 
$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');によって"国フィールドの選択を結びつけるために

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

Source

は、テンプレートに次のコードを使用する他のいくつかの形式でWoocommerce国/州フィールドを追加するには

+0

は、私は、カスタム登録フォームを実装するためにそれを使用し 、私はカスケードに状態や地域のフィールドを走る部分のJSを探していた 、ありがとうございました。 –

2

このソースで処理されています州のフィールドオプションを使用すると、これらのフィールドをクラス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); 
?> 
+0

ありがとう、 woocommerceで有効になっているフィルタのみを取得するのに便利なフィルタが見つかりました。 –

関連する問題