0
votes

I'm creating a simple plugin that lets me to save some input text fields while the user is registering on the site. This part works well.

But I want to let the users edit this fields on the user account page. I can load the saved info on the database but I can't update this info on the the user metadata.

This code puts the input on the register page, the others 4 possible inputs are dynamically added with jquery if the user clicks on the + button.

add_action ('register_form', 'save_dynamic_inputs', 10);

function save_dynamic_inputs() {

$user_cnpj = ( isset( $_POST['user_cnpj_1'] ) ) ? $_POST['user_cnpj_1'] : '';
$user_cnpj = ( isset( $_POST['user_cnpj_2'] ) ) ? $_POST['user_cnpj_2'] : '';
$user_cnpj = ( isset( $_POST['user_cnpj_3'] ) ) ? $_POST['user_cnpj_3'] : '';
$user_cnpj = ( isset( $_POST['user_cnpj_4'] ) ) ? $_POST['user_cnpj_4'] : '';

?>

<div class="cnpjs"><p><label for="user_cnpj_1"><?php _e( 'CNPJ', 'save_dynamic_inputs' ) ?><br /><input type="text" name="user_cnpj_1" id="user_cnpj_1" class="input" style="width: 445px; value="<?php echo esc_attr( stripslashes( $user_cnpj ) ); ?>" size="19" /><i class="button addmore fa fa-plus" aria-hidden="true"></i></label></p></div>

<?php
}

This another code saves every input if they exist in the user metadata, and this code works, but it's for the register form:

add_action( 'user_register', 'saving_my_dynamic_inputs', 10, 1 );

function saving_my_dynamic_inputs( $user_id ) {

for ($i = 1; $i < 5; $i++) {
    if ( isset( $_POST['user_cnpj_'.$i] ) )
    update_user_meta($user_id, 'user_cnpj_'.$i, $_POST['user_cnpj_'.$i]);
}

}

And this is the code that I'm using to update the user metadata on the user edit account page, this code does not work.

add_action('woocommerce_created_customer', 'update_fields_profile_user_woo', 10, 1);

function update_fields_profile_user_woo($customer_id) {

if ( isset( $_POST['user_cnpj_1'] ) ) {
update_user_meta($customer_id, 'user_cnpj_1', $_POST['user_cnpj_1']);
} else {
    echo "<pre>",print_r($_POST),"</pre>";
    exit();
}

}

Where you can read woocommerce_created_customer, I have tried woocommerce_save_account_details and others.

I don't understand why the inputs does not update the user metadata. I have tried to point the form to a php file that gets the $_POST var and prints this, and the array have all the inputs like the sample above:

array

Like I said, the user account profile loads all the inputs needed, but this last code does not update the info on user metadata.

1
What code are you running on woocommerce_save_account_details? That looks like the correct hook.helgatheviking
@helgatheviking I tryed the woocommerce_save_account_details here in this code, but changed woocommerce_created_customer for woocommerce_save_account_details of course, but the user metadata does not get updated: add_action('woocommerce_created_customer', 'update_fields_profile_user_woo', 10, 1); function update_fields_profile_user_woo($customer_id) { if ( isset( $_POST['user_cnpj_1'] ) ) { update_user_meta($customer_id, 'user_cnpj_1', $_POST['user_cnpj_1']); } else { echo "<pre>",print_r($_POST),"</pre>"; exit(); } }Ramon Carvalho
Sorry, see this pastbin pastebin.com/M4E1f5qE Is the same code aboveRamon Carvalho

1 Answers

1
votes

Woocommerce handles all forms through Form Form Handler Class. You need to create an instance of that class and call its save_account_details method.

Another way to hook it with wp. It will be always invoked at the time of initialization.

function save_wc_additional_details() {
$user_id = get_current_user_id();
if( isset( $_POST['user_cnpj_1'] ) )
    update_user_meta( $user_id, 'user_cnpj_1', sanitize_text_field(  $_POST['user_cnpj_1'] ) );
if( isset( $_POST['user_cnpj_2'] ) )
    update_user_meta( $user_id, 'user_cnpj_2', sanitize_text_field( $_POST['user_cnpj_1'] ) );
if( isset( $_POST['user_cnpj_3'] ) )
    update_user_meta( $user_id, 'user_cnpj_3', sanitize_text_field( $_POST['user_cnpj_1'] ) );
if( isset( $_POST['user_cnpj_4'] ) )
    update_user_meta( $user_id, 'user_cnpj_4', sanitize_text_field( $_POST['user_cnpj_1'] ) );
}

add_action( 'wp', 'save_wc_additional_details' );