How to add Billing fields on Woocommerce’s Edit-Account page! (Wordpress)

Asked

Viewed 726 times

2

Guys how can I add fields that are already default from Woocommerce on my account page? I tried this: but does not object to the expected outcome.

add_action( 'woocommerce_edit_account_form', 'add_fields_to_edit_account_form');
function add_fields_to_edit_account_form( $fields ) {
    $fields['billing']['billing_cpf'] = array(
        'label'         => __('CPF', 'woocommerce'),
        'placeholder'   => _x('CPF', 'placeholder', 'woocommerce'),
        'required'      => true,
        'class'         => array('form-row-last'),
     );

    return $fields;
}

Thank you!

1 answer

1


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' ); ?>&nbsp;<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!

  • 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.

  • I get it... I added another hook to validate... I just didn’t make a more advanced condition with regard to CPF.

  • Blz Brother.. I’m going to test it here.. I believe it’s something very similar to this! Thank you.

  • 1

    Anything, we got it! ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.