WooCommerce结帐字段的自定义验证 [英] Custom validation of WooCommerce checkout fields

查看:69
本文介绍了WooCommerce结帐字段的自定义验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加自己的正则表达式来验证电话号码。在我的 class-wc-validation.php 中,将正则表达式更改为我的要求。

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 requirement.

public static function is_phone( $phone ) {
    //if ( strlen( trim( preg_replace( '/[\s\#0-9_\-\+\(\)]/', '', $phone ) ) ) > 0 )
    if ( strlen( trim( preg_replace( '/^[6789]\d{9}$/', '', $phone ) ) ) > 0 )
        return false;

    return true;
}

但是验证没有发生。我缺少什么?

But the validation is not happening. What am I missing?

推荐答案

我还没有看到您的将这些与woocommerce结帐流程挂钩的代码。
请在

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

woocommerce_checkout_process woocommerce_checkout_order_processed

但是,根据您的情况,我强烈建议您将其连接到 woocommerce_checkout_process

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

因此将以下代码放在主题的functions.php下方,或者创建自己的woocommerce插件,并将其放在引导代码中。

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

这篇关于WooCommerce结帐字段的自定义验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆