• Resolved Qilin

    (@qilin2000)


    I have a shipping phone field in my shipping address section, and below is the code I validate the phone numbers customers fill in.

    add_action( 'woocommerce_checkout_process', 'custom_shipping_phone_validation' );
    add_action( 'woocommerce_after_save_address_validation', 'custom_shipping_phone_validation' );
    function custom_shipping_phone_validation() {
                $is_correct = preg_match('/^1[0-9]{10}$/', $_POST['shipping_phone']);
                if ( $_POST['shipping_phone'] && !$is_correct) {
                    wc_add_notice( __( 'The phone number should be 11 digits and start with 1.' ), 'error' );
             }
        }

    It works fine. But when adding a new shipping address on the my account page, the validation above doesn’t work properly, because the name of shipping phone field changes to ‘shipping2_phone’, and there will be ‘shipping3_phone’, ‘shipping4_phone’, ‘shipping5_phone’, etc. if more shipping addresses are added into the section, so how to modify the code above to validate the phone numbers? I’m not very familiar with php language, any help would be greatly appreciated.

    • This topic was modified 4 years, 2 months ago by Qilin.
    • This topic was modified 4 years, 2 months ago by Qilin.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Matt Harrison

    (@matt-h)

    I just threw this together, but this might work. It checks for the address book key and sets the field key appropriately.

    
    add_action( 'woocommerce_checkout_process', 'custom_shipping_phone_validation' );
    add_action( 'woocommerce_after_save_address_validation', 'custom_shipping_phone_validation' );
    function custom_shipping_phone_validation() {
    	if ( empty( $_GET['address-book'] ) ) {
    		$key = 'shipping_phone';
    	} else {
    		$key = $_GET['address-book'] . '_phone';
    	}
    	if ( ! isset( $_POST[ $key ] ) ) {
    		return;
    	}
    	$is_correct = preg_match( '/^1[0-9]{10}$/', $_POST[ $key ] );
    	if ( empty( $_POST[ $key ] ) || ! $is_correct ) {
    		wc_add_notice( __( 'The phone number should be 11 digits and start with 1.' ), 'error' );
    	}
    }
    
    Thread Starter Qilin

    (@qilin2000)

    Hello Matt,

    Thank you so much, your code works like a charm, my problem is resolved!

    Best regards,
    Qilin

    Thread Starter Qilin

    (@qilin2000)

    Hi Matt, there is still a little problem. My shipping phone field is optional, with your code above, a new shipping address on the my account page cannot be saved without a shipping phone, what should we do?

    Plugin Author Matt Harrison

    (@matt-h)

    Probably a check if it is empty before verifying would work.

    This maybe:

    
    add_action( 'woocommerce_checkout_process', 'custom_shipping_phone_validation' );
    add_action( 'woocommerce_after_save_address_validation', 'custom_shipping_phone_validation' );
    function custom_shipping_phone_validation() {
    	if ( empty( $_GET['address-book'] ) ) {
    		$key = 'shipping_phone';
    	} else {
    		$key = $_GET['address-book'] . '_phone';
    	}
    	if ( empty( $_POST[ $key ] ) ) {
    		return;
    	}
    	$is_correct = preg_match( '/^1[0-9]{10}$/', $_POST[ $key ] );
    	if ( empty( $_POST[ $key ] ) || ! $is_correct ) {
    		wc_add_notice( __( 'The phone number should be 11 digits and start with 1.' ), 'error' );
    	}
    }
    
    
    Thread Starter Qilin

    (@qilin2000)

    Hi Matt, you are awesome, the new code works flawlessly, thank you very much!

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Shipping address validation’ is closed to new replies.