Reputation: 352
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
Upvotes: 2
Views: 6270
Reputation: 23
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 ;
}
Upvotes: 0
Reputation: 49
validation of custom fields at edit account page can be done using this action hook, woocommerce_save_account_details_errors
Upvotes: 1
Reputation: 11808
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' ),'');
}
}
Upvotes: 2