Custom validation of WooCommerce checkout fields

后端 未结 4 1846
孤城傲影
孤城傲影 2021-01-02 09:41

I would like add my own regular expression for validating the phone number. In my class-wc-validation.php I have changed the regular expression to my requiremen

4条回答
  •  孤独总比滥情好
    2021-01-02 10:18

    Was facing the same issue and followed what others had said here, but Woocommerce only sets the errors on validation after woocommerce_checkout_process hook.

    But, in the latest Woocommerce 3.0 (not sure if this is in the 2.x version), we can use the woocommerce_after_checkout_validation hook and then look into the $data param if you are using the standard checkout fields or use $_POST if you have custom fields that aren't added in the standard Woocommerce way. An example of the code is:

    public function validate($data,$errors) { 
        // Do your data processing here and in case of an 
        // error add it to the errors array like:
            $errors->add( 'validation', __( 'Please input that correctly.' ));
    }
    add_action('woocommerce_after_checkout_validation', 'validate',10,2);
    

    Hope that helps!

提交回复
热议问题