2017-10-05 11 views
1

これまで同様の質問がありましたが、私はすべての解決策を試しましたが、何らかの理由で問題なく動作します。WooCommerceと電話フィールド検証の問題でカスタム登録フィールドを追加

私はユーザーが簡単に登録できるように私のサイトのフッターにミニWoocommerce登録フィールドを持っています。電話欄は必須ですが、偽の数字を入力する人の数を減らすために、最小の長さを設定します。

私はfunctions.phpに次のコードを追加しました。(私はあなたが理解しやすいように、フォームへのすべての通知を含めています)プレースホルダとすべてが動作しますが、最小の長さを取得できません「パターン」カスタム属性を使用して)作業します。

誰かが私の手助けをすることができれば、非常に感謝しています。

このフォームは私のウェブサイトのフッターに含まれています

add_filter('woocommerce_checkout_fields' , 'custom_override_checkout_fields'); 
    function custom_override_checkout_fields($fields) 
    {   

    $fields['billing']['billing_phone']['custom_attributes'] = array("pattern" => ".{10,10}"); 
     return $fields;  
    } 

そして、これはのfunctions.phpの残りの部分である:wondercatspopup.com

私が追加したコードがある

/** 
* To add WooCommerce registration form custom fields. 
*/ 

function text_domain_woo_reg_form_fields() { 
    ?> 
    <div class="formumuz" style="display:flex;"> <p class="form-row form-row-first"> 
     <label style="display:none!important;" for="billing_first_name"><?php _e('First name', 'woocommerce'); ?><span class="required">*</span></label> 
     <input style="width: 130px; 
    display: inline-block; margin-right:1px;" type="text" class="woocommerce-Input woocommerce-Input--text input-text placeholder" placeholder="İsim/Name *" type="text" class="input-text" name="billing_first_name" id="billing_first_name" value="<?php if (!empty($_POST['billing_first_name'])) esc_attr_e($_POST['billing_first_name']); ?>" /> 


     <label style="display:none!important;" for="billing_last_name"><?php _e('Last name', 'woocommerce'); ?><span class="required">*</span></label> 
     <input style="width: 130px; 
    display: inline-block; margin-left:1px;" type="text" class="woocommerce-Input woocommerce-Input--text input-text placeholder" placeholder="Soyisim/Surname *" type="text" class="input-text" name="billing_last_name" id="billing_last_name" value="<?php if (!empty($_POST['billing_last_name'])) esc_attr_e($_POST['billing_last_name']); ?>" /> 
    </p></div> 

    <p style="margin-bottom: 0px; margin-top: 10px;" class="form-row form-row-wide"> 

      <label style="display:none!important;" for="reg_billing_phone"><?php _e('Phone', 'woocommerce'); ?></label> 
      <input style="width:254px!important;" type="text" class="woocommerce-Input woocommerce-Input--text input-text placeholder" placeholder="Cep Telefonu/Mobile *" value="+905" name="billing_phone" id="reg_billing_phone" value="<?php esc_attr_e($_POST['billing_phone']); ?>" /> * 
     </p><br> 

    <div class="clear"></div> 
    <?php 
} 

add_action('woocommerce_register_form_start', 'text_domain_woo_reg_form_fields'); 


/** 
* To validate WooCommerce registration form custom fields. 
*/ 
function text_domain_woo_validate_reg_form_fields($username, $email, $validation_errors) { 
    if (isset($_POST['billing_first_name']) && empty($_POST['billing_first_name'])) { 
     $validation_errors->add('billing_first_name_error', __('İsim alanı zorunludur!/Name field is required!', 'woocommerce')); 
    } 

    if (isset($_POST['billing_last_name']) && empty($_POST['billing_last_name'])) { 
     $validation_errors->add('billing_last_name_error', __('Soyisim alanı zorunludur!/Surname field is required!', 'woocommerce')); 
    } 

    if (isset($_POST['billing_phone']) && empty($_POST['billing_phone'])) { 
     $validation_errors->add('billing_phone_error', __('Telefon alanı zorunludur!/Phone field is required!', 'woocommerce')); 
    } 


    return $validation_errors; 
} 




add_action('woocommerce_register_post', 'text_domain_woo_validate_reg_form_fields', 10, 3); 

/** 
* To save WooCommerce registration form custom fields. 
*/ 
function text_domain_woo_save_reg_form_fields($customer_id) { 
    //First name field 
    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'])); 
    } 
    //Last name field 
    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'])); 
    } 
    //Phone field 
    if (isset($_POST['billing_phone'])) { 
     update_user_meta($customer_id, 'phone', sanitize_text_field($_POST['billing_phone'])); 
     update_user_meta($customer_id, 'billing_phone', sanitize_text_field($_POST['billing_phone'])); 
    } 


} 

答えて

1

注:フッターにある特別登録フォームは、WooCommerceのチェックアウトフィールドとは異なるものです。

あなたのコードでは、検証機能のフックtext_domain_woo_validate_reg_form_fields()が欠落しています。また、いくつかの小さな誤差あります(修正) ...

提出されたデータは、バリデーション機能であり、あなたはまた、があまりにチェックアウトために別のものを追加する必要があるのを確認します(代わりにするのに最適な場所データをフォーマットするために使用されるチェックアウト電話欄にカスタムパターンを使用)

だからあなたの関連するコードがあるべきすべて:

## --- FOR CHECKOUT --- ## 

// Checkout billing phone validation (Checking length) 
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process'); 
function my_custom_checkout_field_process() { 
    if ($_POST['billing_phone'] && strlen($_POST['billing_phone']) < 10) 
     wc_add_notice(__('Please type a correct phone number…', 'woocommerce'), 'error'); 
} 

## --- FOR CUSTOM REGISTRATION FORM --- ## 

// Add custom fields to registration form. 
add_action('woocommerce_register_form_start', 'text_domain_woo_reg_form_fields'); 
function text_domain_woo_reg_form_fields() { 
    ?> 
    <div class="formumuz" style="display:flex;"> 
     <p class="form-row form-row-first"> 
      <label style="display:none!important;" for="billing_first_name"><?php _e('First name', 'woocommerce'); ?><span class="required">*</span></label> 
      <input style="width: 130px; display: inline-block; margin-right:1px;" type="text" class="woocommerce-Input woocommerce-Input--text input-text placeholder" placeholder="İsim/Name *" type="text" class="input-text" name="billing_first_name" id="billing_first_name" value="<?php if (!empty($_POST['billing_first_name'])) esc_attr_e($_POST['billing_first_name']); ?>" /> 
     </p> 
     <p class="form-row form-row-last"> 
      <label style="display:none!important;" for="billing_last_name"><?php _e('Last name', 'woocommerce'); ?><span class="required">*</span></label> 
      <input style="width: 130px; display: inline-block; margin-left:1px;" type="text" class="woocommerce-Input woocommerce-Input--text input-text placeholder" placeholder="Soyisim/Surname *" type="text" class="input-text" name="billing_last_name" id="billing_last_name" value="<?php if (!empty($_POST['billing_last_name'])) esc_attr_e($_POST['billing_last_name']); ?>" /> 
     </p> 
    </div> 
    <p style="margin-bottom: 0px; margin-top: 10px;" class="form-row form-row-wide"> 
     <label style="display:none!important;" for="reg_billing_phone"><?php _e('Phone', 'woocommerce'); ?></label> 

     <!-- "You can’t have 2 times the value attribute and you can use "tel" type … (to be removed)" --> 
     <input style="width:254px!important;" type="tel" class="woocommerce-Input woocommerce-Input--text input-text placeholder" placeholder="Cep Telefonu/Mobile *" name="billing_phone" id="reg_billing_phone" value="<?php esc_attr_e($_POST['billing_phone']); ?>" /> * 
    </p><br> 
    <div class="clear"></div> 
    <?php 
} 

// Checking & validation of custom fields in registration form. 
add_action('woocommerce_register_post', 'text_domain_woo_validate_reg_form_fields', 10, 3); 
function text_domain_woo_validate_reg_form_fields($username, $email, $validation_errors) { 
    if (isset($_POST['billing_first_name']) && empty($_POST['billing_first_name'])) { 
     $validation_errors->add('billing_first_name_error', __('İsim alanı zorunludur!/Name field is required!', 'woocommerce')); 
    } 
    if (isset($_POST['billing_last_name']) && empty($_POST['billing_last_name'])) { 
     $validation_errors->add('billing_last_name_error', __('Soyisim alanı zorunludur!/Surname field is required!', 'woocommerce')); 
    } 
    if (isset($_POST['billing_phone']) && empty($_POST['billing_phone'])) { 
     $validation_errors->add('billing_phone_error', __('Telefon alanı zorunludur!/Phone field is required!', 'woocommerce')); 
    } 

    // ==> CHECKING PHONE LENGTH (10 character minimal) <== 
    if (isset($_POST['billing_phone']) && strlen($_POST['billing_phone']) < 10) { 
     $validation_errors->add('billing_phone_error', __('Please type a correct phone number…', 'woocommerce')); 
    } 
    return $validation_errors; 
} 

// Add custom fields to registration form. 
add_action('woocommerce_created_customer', 'custom_save_extra_register_fields'); // <==== Missing 
function custom_save_extra_register_fields($customer_id) { 
    //First name field 
    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'])); 
    } 
    //Last name field 
    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'])); 
    } 
    //Phone field 
    if (isset($_POST['billing_phone'])) { 
     update_user_meta($customer_id, 'phone', sanitize_text_field($_POST['billing_phone'])); 
     update_user_meta($customer_id, 'billing_phone', sanitize_text_field($_POST['billing_phone'])); 
    } 
} 

コードは、あなたのアクティブな子テーマ(またはテーマ)のfunction.phpファイルやも任意のプラグインファイルになります。

テストおよび電話番号、最小限の長さがをチェックし、(必要であれば)このアラートが表示されます

たびを作品

enter image description here

+0

は完全に働いたこと、本当にありがとうございました!もう1つの最後の質問:上記のコードでは、2つの名字フィールドが表示されています(上のリンクに表示されています)。これも修正する方法を教えてください。 – eylul

+0

@eylul私は私の答えコードでこの問題を抱えていません...これは、これらのフィールドのID「reg_sr_firstname」と「reg_sr_lastname」で行った他のカスタマイズによるものです。 – LoicTheAztec

関連する問題