Reputation:
I have successfully hooked into the My Account page using woocommerce_edit_account_form and woocommerce_save_account_details, and that seems to be working fine.
The only issue is that I have a field for a mobile phone number that I need to validate to 10 digits (if the mobile notification checkbox is selected), and I cannot seem to find out exactly how and where to add the code.
Wordpress is new to me but I have PHP coding experience. I did several Google searches, and it looked like woocommerce_save_account_details_errors is the hook I need, but I cannot find any specific samples with the correct syntax to get it to work.
I tried this with no luck (page saved without error message):
function my_woocommerce_save_account_details_errors( $user_id ) {
if ( isset( $_POST['mobile_number'] ) )
{
if(strlen($_POST['mobile_number'])<6 )
$args->add( 'error', __( 'Your mobile number must be exactly 10 digits long (5551112222)', 'woocommerce' ),'');
}
}
Any help would be greatly appreciated, -Ben
Upvotes: 1
Views: 1636
Reputation: 191
try to adapt your code with this one:
add_action( 'woocommerce_save_account_details_errors','wdm_validate_custom_field', 10,2 );
function wdm_validate_custom_field(&$args, &$user)
{
checkFieldEmpty('your_field', 'Label_of_the_Field');
}
function checkFieldEmpty($field_name, $field_label){
if ( empty( $_POST[$field_name] ) )
{
wc_add_notice( '<strong>' . esc_html( $field_label ) . '</strong> ' . __( 'is a required field.', 'woocommerce' ), 'error' );
}
}
Upvotes: 2