Woocommerce: validate custom fields on my account

2019-07-20 09:08发布

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

3条回答
欢心
2楼-- · 2019-07-20 09:22

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' ),'');
    }
}
查看更多
\"骚年 ilove
3楼-- · 2019-07-20 09:30

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 ; 
} 
查看更多
别忘想泡老子
4楼-- · 2019-07-20 09:38

validation of custom fields at edit account page can be done using this action hook, woocommerce_save_account_details_errors

查看更多
登录 后发表回答