WooCommerce结帐字段的自定义验证 [英] Custom validation of WooCommerce checkout fields
问题描述
我想添加自己的正则表达式来验证电话号码。在我的 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 $ c中查看其文档$ c>
但是,根据您的情况,我强烈建议您将其连接到 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屋!