简体   繁体   English

WooCommerce验证billing_address_1结帐字段

[英]WooCommerce validate billing_address_1 checkout field

I am trying to validate one of our WooCommerce checkout fields, namely billing_address_1. 我正在尝试验证我们的WooCommerce结帐字段之一,即billing_address_1。

Right now, the field will accept any address but it should check to see if the address field contains a number before processing. 现在,该字段将接受任何地址,但是在处理之前,应检查地址字段是否包含数字。 Not sure how this should be done in WooCommerce. 不知道该如何在WooCommerce中完成。 I will also accept any other solution which are non WooCommerce. 我还将接受非WooCommerce的任何其他解决方案。

The website is: https://www.prikkabelled.nl 该网站是: https : //www.prikkabelled.nl

You should be looking at this field at checkout: 您应该在结帐时查看此字段:

在此处输入图片说明

You can use custom validation for this. 您可以为此使用自定义验证。 Change below code as per your requirement: 根据您的要求更改以下代码:

add_action('woocommerce_checkout_process', 'is_phone');

function is_phone() { 
    $addr1 = $_POST['billing_address_1'];
    // check condition if number not found in address
    if(false)
        wc_add_notice( __( 'Address does not contain a number.' ), 'error' );
}

Here is the step you can add to the checkout progress via functions.php 这是您可以通过functions.php添加到结帐进度的步骤

add_action('woocommerce_checkout_process', 'custom_checkout_field_check');

function custom_checkout_field_check() {
    // Check if set, if its not set add an error.
    if ( $_POST['billing_address_1'] && strpbrk($_POST['billing_address_1'], '1234567890') !== FALSE )
        wc_add_notice( __( 'Your address field contains numeric value' ), 'error' );
}

This was the answer I was looking for which I got straight from the WooCommerce developer community. 这是我一直在寻找的答案,我直接从WooCommerce开发人员社区获得了。

// Check if address field contains house number otherwise provide error message

add_action( 'woocommerce_after_checkout_validation', 'validate_checkout', 10, 2); 

function validate_checkout( $data, $errors ){ 
    if (  ! preg_match('/[0-9]/', $data[ 'billing_address_1' ] ) ){ 
        $errors->add( 'address', 'Sorry, but the address you provided does not contain a house number.' ); 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM