简体   繁体   中英

Woocommerce: validate custom fields on my account edit page

I need to validate and save my custom fields in "Edit my account" page. I found template of this page at woocommerce/myaccount/form-edit-account.php . I managed to add my custom fields in this template file. Now I need to validate them and if it's all good - save user data.

What hook do I need to use in this situation? I can edit class-wc-form-handler.php but I don't want to.

Thanks

I found out an action hook that can be used to validate custom fields or existing fields on Edit Account page.
Add following code in functions.php.

add_action('user_profile_update_errors','wdm_validate_custom_field',10,1);
function wdm_validate_custom_field($args)
{
    if ( isset( $_POST['custom_field'] ) )
    {
        if(strlen($_POST['custom_field'])<6 )
        $args->add( 'error', __( 'Your error message', 'woocommerce' ),'');
    }
}

可以使用此操作挂钩 woocommerce_save_account_details_errors 在编辑帐户页面验证自定义字段

Try This.

add_filter( 'woocommerce_process_myaccount_field_{your_custom_field_name}' , 'validate_edit_address' );
function validate_edit_address() {

    $variable = $_POST['your_custom_field_name'];
    if ( preg_match("/[ก-๙a-zA-Z]+$/", $variable ) ){       
        wc_add_notice( __( '<strong>Error</strong>' ), 'error' );
    }
    return $variable ; 
}  

Example :

add_filter( 'woocommerce_process_myaccount_field_billing_house_number' , 'validate_edit_address' );
function validate_edit_address() {

    $variable = $_POST['billing_house_number'];
    if ( preg_match("/[ก-๙a-zA-Z]+$/", $variable ) ){       
        wc_add_notice( __( '<strong>Error</strong>' ), 'error' );
    }
    return $variable ; 
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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