user2610975
user2610975

Reputation: 31

Update user meta after woocommerce checkout form process

I am using woocommerce with Wordpress and have added some custom fields to the checkout:

add_action('woocommerce_after_order_notes', 'my_custom_checkout_field');

function my_custom_checkout_field( $checkout ) {

      $extra_fields = array('job_title', 'company', 'telephone', 'occupation');

      foreach($extra_fields as $key => $value) {

      woocommerce_form_field($value, array(
            'type'          => 'text',
            'class'         => array('my-field-class form-row-wide'),
            'label'         => __($label),
            'value'         => '',
            ), $checkout->get_value( $value ));

      }
}

Now currently, these appear in the checkout fine, not sure if using woocommerce_after_order_notes is right in this case. I have also added some custom fields to the user meta that correspond to the fields added to the checkout - which all display in the user profile page:

function add_contact_methods( $contactmethods ) {
     $contactmethods['job_title'] = 'Job Title';
     $contactmethods['company'] = 'Company Name';
     $contactmethods['telephone'] = 'Telephone';
     $contactmethods['occupation'] = 'Occupation';
     $contactmethods['refer'] = 'How you heard about us?';
     return $contactmethods;
}
add_filter('user_contactmethods','add_contact_methods',10,1);

As you can imagine, if I update any of these field in any profile page, it works fine but what I cant seem to do is update the user meta when a new user makes a purchase, it does not update the user meta for these fields in the database.

I understand alot of how this works, and understand that I must hook into a Woocommerce process to add the fields into the process. So I have added this code into my functions too:

add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta');

    function my_custom_checkout_field_update_user_meta( $user_id ) {
        global $extra_fields;
        foreach($extra_fields as $key => $value) {
        if ($_POST[$value]) update_user_meta( $user_id, $value, esc_attr($_POST[$value]));
        }
    }

Now the twist is, this works if a user who is already signed in as a member, makes a repurchase and goes through the checkout - the reason this works is because $user_id already exists, but when a new user is checking out, they do not yet exist as a user, hence the function cannot update the user meta of NIL where $user_id does not exist.

My question is, how do I hook into the checkout process, presumably AFTER the user has been created, so I that I can get the $user_id returned, and execute this function to update the user meta.

Upvotes: 3

Views: 8359

Answers (2)

user5200704
user5200704

Reputation: 1709

When customer is not logged in checkout page should be acceptable field customer want to create a new account.Below sample code change in checkout page when customer order a new item and update user meta data.

function user_extra_meta_fields(){

 return array(
   'job_title' => __( 'Job Title', 'yourtext_domain'),
   'company' => __( 'Company Name', 'yourtext_domain'),
   'telephone' => __( 'Telephone', 'yourtext_domain'),
   'occupation' => __( 'Occupation', 'yourtext_domain'),
   'refer' => __( 'How you heard about us?', 'yourtext_domain'),
 ); 

} 

function add_contact_methods( $contactmethods ) {
     $contactmethods = array_merge( $contactmethods, user_extra_meta_fields());
     return $contactmethods;
}

add_filter('user_contactmethods','add_contact_methods',10,1);


add_action('woocommerce_after_order_notes', 'my_custom_checkout_field');

function my_custom_checkout_field( $checkout ) {

  foreach( user_extra_meta_fields() as $name => $label) {
     $value = '';     
     if( is_user_logged_in() )
     $value = get_user_meta( get_current_user_id(), $name, true );

      woocommerce_form_field( $name, array(
            'type'          => 'text',
            'class'         => array('my-field-class form-row-wide'),
            'label'         => $label,
            ), $value );

      }
}

add_action( 'woocommerce_checkout_process', 'user_fields_woocommerce_checkout_process' );

function user_fields_woocommerce_checkout_process(){

  if( is_user_logged_in() )
  add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta' );

  else 
  add_action( 'woocommerce_created_customer',  'my_custom_checkout_field_update_user_meta' );
}


function my_custom_checkout_field_update_user_meta( $user_id ) {

    foreach( array_keys( user_extra_meta_fields() ) as $meta_name  ){
      if( isset( $_POST[$meta_name] ) ){
         $meta_value = $_POST[$meta_name] ? esc_attr($_POST[$meta_name]) : '';                                                    
         update_user_meta( $user_id,  $meta_name, $meta_value );  
      }

    }
}

// if want to validate field

add_action( 'woocommerce_after_checkout_validation', 'user_fields_woocommerce_after_checkout_validation' );

function user_fields_woocommerce_after_checkout_validation( $posted ){

  $validate = true;
  if( ! is_user_logged_in() && empty( $posted['createaccount'] )  )
  $validate = false;

  if( $validate == false )
  return; 

  $meta_data = user_extra_meta_fields();

  foreach( array_keys( $meta_data ) as $meta_name  ){
    if(  empty($_POST[$meta_name]) )
    wc_add_notice( sprintf( __(' <strong>%s</strong> is required.', 'yourtext_domain'),  $meta_data[$meta_name] ), 'error' );  
  }

} 

Upvotes: 2

Aryan Duntley
Aryan Duntley

Reputation: 21

class-wc-checkout.php line 639 creates the new user with $this->customer_id = wp_insert_user( apply_filters( 'woocommerce_new_customer_data', $new_customer_data ) ); The new customer data is an array listed just above that line.

Following that, you can access the user id with line 649's action do_action( 'woocommerce_created_customer', $this->customer_id );

It is unlikey, in your case, you will need to use the filter, but simply add the action 'woocommerce_created_customer', pull in the id, and add the meta.

Upvotes: 2

Related Questions