If this CPF is coming through the plugin Woocommerce Extra Checkout Fields, the method via Billing in an array will probably not understand this information in woocommerce_edit_account_form. The easiest but least dynamic way would be to pull this information through the function wp_get_current_user(). Below you can see how I did (by the way, I followed the same format of form-Edit-Account.php of the Woocommerce):
//add cpf field
function add_cpf_field() {
$user = wp_get_current_user();
?><p class="woocommerce-form-row woocommerce-form-row--first form-row form-row-first">
<label for="billing_cpf"><?php esc_html_e( 'CPF', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="billing_cpf" id="billing_cpf" value="<?php echo esc_attr( $user->billing_cpf ); ?>" />
</p><?php
}
add_action( 'woocommerce_edit_account_form', 'add_cpf_field');
//save cpf field
function save_cpf_details( $user_id ){
$cpf = ! empty( $_POST['billing_cpf'] ) ? wc_clean( $_POST['billing_cpf'] ) : '';
update_user_meta( $user_id, 'billing_cpf', $cpf );
}
add_action( 'woocommerce_save_account_details', 'save_cpf_details' );
//validate cpf field
function validate_cpf( $validation_error ) {
if ( isset( $_POST['billing_cpf'] ) ) {
if(strlen($_POST['billing_cpf']) < 11 ) //condição básica de pelo menos 11 caracteres
$validation_error->add( 'error', __( 'Your CPF is not valid.', 'woocommerce' ),'');
}
}
add_action( 'woocommerce_save_account_details_errors','validate_cpf', 10, 1 );
I hope I’ve helped! ;)
Yes more anyway it does not take the required field settings and does not save the data in the database. thanks!
– wDrik
What do you mean, setting mandatory fields? Anyway, I updated the code and added a hook to save the CPF field, so if the person wants to change right on the account page, now they can.
– Jandeilson De Sousa
I get it... I added another hook to validate... I just didn’t make a more advanced condition with regard to CPF.
– Jandeilson De Sousa
Blz Brother.. I’m going to test it here.. I believe it’s something very similar to this! Thank you.
– wDrik
Anything, we got it! ;)
– Jandeilson De Sousa