Custom validation of WooCommerce checkout fields

后端 未结 4 1816
孤城傲影
孤城傲影 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:23

    I have not seen your code that hooks these up to the woocommerce checkout flow. please check their documentation on

    woocommerce_checkout_process and woocommerce_checkout_order_processed

    But in your case, I highly suggest that you hook it up on woocommerce_checkout_process

    so put these codes below on your functions.php on your theme, or you create your own woocommerce plugins, and put it in the bootstrap code.

    add_action('woocommerce_checkout_process', 'is_phone');
    
    function is_phone() { 
        $phone_number = $_POST['---your-phone-field-name---'];
        // your function's body above, and if error, call this wc_add_notice
        wc_add_notice( __( 'Your phone number is wrong.' ), 'error' );
    }
    

提交回复
热议问题