2017-06-19 4 views
0

私はwoocommerce登録ページに3つのカスタムフィールドを追加しました。これまでワードプレスで電話番号をユニークにする方法

私のコード: -

/* Add Extra Registration Field in Woocommerce */ 

//Add First Name, Last Name, and Phone Number 
function woocom_extra_register_fields() {?> 
     <p class="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 esc_attr_e($_POST['billing_phone']); ?>" /> 
     </p> 
     <p class="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="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="clear"></div> 
     <?php 
} 
add_action('woocommerce_register_form_start', 'woocom_extra_register_fields'); 


//Validate Fields 
function woocom_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', __('<strong>Error</strong>: First name is required!', 'woocommerce')); 

     } 

     if (isset($_POST['billing_last_name']) && empty($_POST['billing_last_name'])) { 

      $validation_errors->add('billing_last_name_error', __('<strong>Error</strong>: Last name is required!.', 'woocommerce')); 

     } 
     if (isset($_POST['billing_phone']) && empty($_POST['billing_phone'])) { 

      $validation_errors->add('billing_phone_error', __('<strong>Error</strong>: Mobile number is required!.', 'woocommerce')); 

     } 
     return $validation_errors; 
} 

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



//Save Fields 
function woocom_save_extra_register_fields($customer_id) { 
    if (isset($_POST['billing_phone'])) { 
       // Phone input filed which is used in WooCommerce 
       update_user_meta($customer_id, 'billing_phone', sanitize_text_field($_POST['billing_phone'])); 
      } 
     if (isset($_POST['billing_first_name'])) { 
      //First name field which is by default 
      update_user_meta($customer_id, 'first_name', sanitize_text_field($_POST['billing_first_name'])); 
      // First name field which is used in WooCommerce 
      update_user_meta($customer_id, 'billing_first_name', sanitize_text_field($_POST['billing_first_name'])); 
     } 
     if (isset($_POST['billing_last_name'])) { 
      // Last name field which is by default 
      update_user_meta($customer_id, 'last_name', sanitize_text_field($_POST['billing_last_name'])); 
      // Last name field which is used in WooCommerce 
      update_user_meta($customer_id, 'billing_last_name', sanitize_text_field($_POST['billing_last_name'])); 
     } 

} 
add_action('woocommerce_created_customer', 'woocom_save_extra_register_fields'); 




/* END Add Extra Registration Field in Woocommerce */ 

すべてが正常に動作しているが、問題はそのがdupliacteの電話番号を受け入れるということです。電話番号フィールドを一意にするにはどうすればいいですか?どんなアイデアも本当に感謝しています。

ありがとうございました。

+0

jQueryのは、重複値を防ぐために、あなたを助ける必要があります。 – jjj

答えて

0

は値が検証を実行する存在する場合meta_valueによってGET_USERS機能を使用してみてください:

if (isset($_POST['billing_phone']) && empty($_POST['billing_phone'])) { 
      $validation_errors->add('billing_phone_error', __('<strong>Error</strong>: Mobile number is required!.', 'woocommerce')); 

     } 
if (isset($_POST['billing_phone'])) { 
    $hasPhoneNumber= get_users('meta_value='.$_POST['billing_phone']); 
    if (!empty($hasPhoneNumber)) { 
    $validation_errors->add('billing_phone_error', __('<strong>Error</strong>: Mobile number is already used!.', 'woocommerce')); 
    } 
} 
+0

ありがとうbuxbeatz ..あなたは私の人生を救った:) –

関連する問題