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


}
LoicTheAztec

注意:页脚中的特殊注册表格与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文件中,或者在任何插件文件中。

经过测试和工作

每次将检查电话的最小长度并显示此警报(如果需要)

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Woocommerce中的条件自定义结帐字段验证问题

在WooCommerce中添加和处理自定义结帐选择字段时出现问题

根据WooCommerce中的类别和自定义文本字段添加到购物车验证

在结帐页面和WooCommerce数据字段中添加取件地点自定义字段

在“设计”注册表单中添加“自定义”字段时遇到问题

在Django中添加自定义用户注册字段

Woocommerce 3中的自定义结帐字段验证

Woocommerce中的自定义增值税字段问题

为注册表单中的自定义字段设计自定义验证

在woocommerce结帐页面自定义字段中添加日期

在Woocommerce中的条款下方添加自定义结帐字段

在Woocommerce编辑帐户页面中添加自定义字段

在 WooCommerce 中添加、更新或删除产品自定义字段

woocommerce BACS 添加自定义字段

在Woocommerce中添加和保存管理产品变体自定义字段

在Woocommerce 3中添加和管理产品自定义上传字段

在 WooCommerce 中为变化添加和显示多个自定义字段

在 WooCommerce 中隐藏自定义字段

WooCommerce 订单电子邮件中未显示自定义电话号码字段

React 中自定义字段的自定义验证

添加自定义注册字段并检查其值

WooCommerce:结帐验证失败后更新自定义字段

如何在Rails 4中添加要设计的自定义字段并自定义注册控制器?

Woocommerce自定义结帐必填字段行为问题

如何在Laravel 5.6的默认注册表单中添加自定义字段?

如何在用户注册 asp.net 样板中添加自定义字段

如何在Magento 2.2的客户注册中添加自定义字段?

向Woocommerce结帐添加新的自定义字段

WooCommerce:随处添加/显示产品或版本自定义字段