• Resolved angeloarlotta

    (@angeloarlotta)


    Hi,
    I added the billing phone field into myaccount/edit-account page by editing the form-edit-account.php template.

    I added those codes to check, validate and save the field but I need also to make this field not required so I added also the code below.

    The problem is that the field can’t be left blank and the validation gives always the error message “Please fill the phone number”.

    How can I keep the validating codes and make the field not required.

    // Check and validate the phone field
    add_action( 'woocommerce_save_account_details_errors','billing_phone_field_validation', 20, 1 );
    function billing_phone_field_validation( $args ){
        if ( isset($_POST['billing_phone']) && empty($_POST['billing_phone']) )
            $args->add( 'error', __( 'Please fill the phone number', 'woocommerce' ),'');
    }
    
    // Save the mobile phone value to user data
    add_action( 'woocommerce_save_account_details', 'my_account_saving_billing_phone', 20, 1 );
    function my_account_saving_billing_phone( $user_id ) {
        if( isset($_POST['billing_phone']) && ! empty($_POST['billing_phone']) )
            update_user_meta( $user_id, 'billing_phone', sanitize_text_field($_POST['billing_phone']) );
    }
    
    //Remove required field requirement
        add_filter('woocommerce_save_account_details_required_fields', 'remove_required_fields');
    
            function remove_required_fields( $required_fields ) {
                unset($required_fields['billing_phone']);
    
                return $required_fields;
    }

    Thank you to anyone can help!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Gabriel – a11n

    (@gabrielfuentes)

    Hi there 👋

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Cheers!

    Plugin Support Gabriel – a11n

    (@gabrielfuentes)

    Since it’s been a while since we last heard back from you, I’m going to mark this thread resolved.

    Hopefully, you were able to find a solution to your problem! If you have further questions, please feel free to open a new topic.

    Cheers! 🙂

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Validation on not required field’ is closed to new replies.