在WooCommerce和手机现场验证问题中添加自定义注册字段

问题描述:

以前也有类似问题,我尝试了所有解决方案,但由于某些原因,它们不适用于我。在WooCommerce和手机现场验证问题中添加自定义注册字段

我有一个迷你Woocommerce登记领域,包括在我的网站的页脚,让人们可以轻松注册。电话字段是必需的,但我想设置一个最小长度来减少输入虚假号码的人数。

我有以下代码添加到我的functions.php,(我包括所有的通知表单,以便您可以更好地理解)占位符和一切正常,但我无法获得最小长度(设置使用“模式”自定义属性)工作。

如果任何人都可以帮助我解决它,它将非常感激。

这种形式包括在我的网站的页脚:wondercatspopup.com

我添加的代码是:

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中的其余部分:

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


} 

注意:位于页脚中的特殊注册表与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

非常感谢你,这完美地工作!最后一个问题:上面的代码现在显示了2个第一个和最后一个名称字段(您可以在上面的链接中看到)。请问如何解决这个问题? – eylul

+0

@eylul我没有这个问题与我的答案代码...所以这是由于其他定制,你已经做了这些字段ID''reg_sr_firstname''和''reg_sr_lastname'' ... – LoicTheAztec