0

WooCommerce shop.

Setting "Customer can register while checkout" is ON, so customer have to fill his Name, Phone and Email while checkout (on /checkout/ page).

I want to check this data for errors before registration. So, I try to get this data from $_REQUEST variable or from $fields array from filter "woocommerce_after_checkout_validation", but I can't do this.

This is how I try:

    add_action( 'woocommerce_before_checkout_registration_form', 'check_request', 1 );
    function check_request () {
        echo '<pre>'; print_r($_REQUEST); echo '</pre>';
    }

    /* OUTPUT:
    Array
    (
        [woocommerce-login-nonce] => 
        [_wpnonce] => 
        [woocommerce-reset-password-nonce] => 
        [woocommerce-edit-address-nonce] => 
        [save-account-details-nonce] => 
    )
    */

And this:

    add_filter( 'woocommerce_after_checkout_validation', 'custom_after_checkout_validation', 10, 2);
    function custom_after_checkout_validation ($fields, $errors){

        if ( empty( $fields['billing_phone'] ) ) {
            $errors->add( 'phone validation', 'Phone is empty' );
        }
    }
    /* OUTPUT:
    Phone is empty
    */

Please, help me.

Viktor
  • 11
  • 1

2 Answers2

0

Try This

add_action('woocommerce_checkout_process', 'customise_checkout_field_process');
function customise_checkout_field_process()
{


  // if the field is set, if not then show an error message.
  if (!$_POST['billing_phone']) wc_add_notice(__('Please enter phone.') , 'error');



}
gaurav sharma
  • 534
  • 2
  • 6
  • Thanks. This works for checking out fields, but I want change them or get some screen output. I want change one html-form field to another. For example, I want to verify users phone by sending SMS to him. So, my plan is 1. Get phone from PHONE field 2. Send SMS to this phone 3. Set $_SESSION variable 'SMS_SENT' 4. Change phone field to SMS field 5. Check SMS from SMS field With this add_action I can't do nothing - I can't set or get – Viktor Mar 04 '20 at 15:49
  • You can verify phone number using SMS service and Ajax Note: You have to integrate variable $flag 0 or 1 with SMS service where you can verify the phone number  If $flag = 0; https://prnt.sc/rbwxhg IF $flag = 1; https://prnt.sc/rbwx1w I think this is helpful for you Please check my post again I have posted new code for you. – gaurav sharma Mar 05 '20 at 10:07
0

You can verify phone number using sms service and Ajax

Past this code in your functions.php

add_action('wp_footer', 'verify_phone_no_wp_footer');
function verify_phone_no_wp_footer(){

    if (is_checkout()) {

        ?>
        <script type="text/javascript">
            jQuery(document).ready(function(){

                jQuery('#billing_phone').after('<div id="verify_phone_no_loader" style="display:none;">Loading..</div>');

                jQuery('#billing_phone').after('<div id="verify_phone_msg"></div>');

                jQuery(document).on('change', '#billing_phone', function(){

                    var target = jQuery(this);

                    var phone_no = target.val();

                    var message = '';

                    var phone_no_r = '';

                    var flag = '';

                    //Ajax
                    jQuery.ajax({
                        url: '<?php echo admin_url( 'admin-ajax.php');?>',
                        type: "POST",
                        data: {'action': 'verify_phone_no_my_action', phone_no: phone_no},
                        cache: false,
                        dataType: 'json',
                        beforeSend: function(){

                            jQuery('#verify_phone_no_loader').show();
                        },
                        complete: function(){

                            jQuery('#verify_phone_no_loader').hide();
                        },
                        success: function (response) { 

                            console.log(response);
                            console.log(response['message']);
                            console.log(response['phone_no']);
                            console.log(response['flag']);

                            message = response['message'];

                            phone_no_e = response['phone_no'];

                            flag = response['flag'];

                            if (flag == 1) {
                                jQuery('#verify_phone_msg').html('<span style="color:green">'+message+'</span>');
                            }else{
                                jQuery('#verify_phone_msg').html('<span style="color:red">'+message+'</span>');
                            }


                        }
                    });
                    //Ajax


                });

            });
        </script>
        <?php

    }

}


add_action( 'wp_ajax_verify_phone_no_my_action', 'verify_phone_no_my_action_function');
add_action( 'wp_ajax_nopriv_verify_phone_no_my_action', 'verify_phone_no_my_action_function');
function verify_phone_no_my_action_function(){

    $phone_no = $_POST['phone_no'];


    //Note: Here you have to add msg service to check the phone is valid or not


    //Also, I have written the temporary condition so that you can understand how to handle it

    $flag = 0;

    if ($flag) { //If flag is '1' phone number is valide

        $myArr = array(
            'message' => 'phone number is valide',
            'phone_no' => $phone_no,
            'flag' => $flag
        );

    }else{ //If flag is 'o' phone number is not valide 

        $myArr = array(
            'message' => 'phone number is not valide',
            'phone_no' => $phone_no,
            'flag' => $flag
        );

    }


    $myJSON = json_encode($myArr); 
    echo $myJSON;
    die();
}

Note: You have to integrate variable $flag 0 or 1 with SMS service where you can verify the phone number 

If $flag = 0; https://prnt.sc/rbwxhg

IF $flag = 1; https://prnt.sc/rbwx1w

I think this is helpful for you

gaurav sharma
  • 534
  • 2
  • 6