skip to Main Content

I am adding woocommerce customer programmatically using wc_create_new_customer function, first name, last name, email is added successfully, but custom fields are not adding.
I have tried two ways
1st one:

$email= $arr_item->email;
enter code here$username = strstr($arr_item->email, ‘@’, true);
$password = wp_generate_password();

$args = array(
“first_name” => $arr_item->first_name,
“billing_first_name” => $arr_item->first_name,
“last_name” => $arr_item->last_name,
“billing_last_name” => $arr_item->last_name,
“billing_email” => $arr_item->email,
“billing_phone” => $arr_item->phone,
“agency_name” => $arr_item->agency_name,
“billing_company” => $arr_item->agency_name,
“agency_country” => $arr_item->agency_country,
“agency_city” => $arr_item->agency_city,
“agency_state” => $arr_item->agency_state
);
$customer_id = wc_create_new_customer( $email, $username, $password, $args );

2nd one

$customer_id = wc_create_new_customer( $email, $username, $password );
update_user_meta( $customer_id, ‘agency_name’,$arr_item->agency_name);
update_user_meta( $customer_id, ‘billing_company’, $arr_item->agency_name);
update_user_meta( $customer_id, ‘agency_country’, $arr_item->agency_country);
update_user_meta( $customer_id, ‘agency_city’, $arr_item->agency_city);
update_user_meta( $customer_id, ‘agency_state’,$arr_item->agency_state);

I am trying to add custom field user meta table while creating a customer using wc_create_new_customer
So that I can access these data using get_user_meta().

Please help if anyone has any ideas.

Thanks in advance

2

Answers


  1. You need to save a customer. Add this 2 lines at the end.

    $customer = get_user_by('id', $customer_id);
    $customer->save();
    
    Login or Signup to reply.
  2. For anybody looking for an answer to this question:

    Overview: To create a new customer for your store you create a WP User using wc_create_new_customer() followed by adding their personal details to their WC_Customer instance.

    Example:

    // Create customer.
    $customer_id = wc_create_new_customer(
      sanitize_email( '[email protected]' ),
      'Password123'
    );
    
    // Check for an error in the creation.
    if ( is_wp_error( $customer_id ) ) {
      throw new ErrorException( $customer_id->get_error_message() );
    }
    
    // Get customers WC_Customer instance.
    $customer = new WC_Customer( $customer_id );
    
    // Set our customer's meta values using WC's CRUD methods.
    $customer->set_first_name( 'Jane' );
    $customer->set_last_name( 'Bloggs' );
    $customer->set_billing_email( '[email protected]' );
    $customer->set_billing_first_name( 'Jane' );
    $customer->set_billing_last_name( 'Bloggs' );
    $customer->set_billing_address_1( 'The Hawthorns' );
    $customer->set_billing_address_2( 'Halfords Lane' );
    $customer->set_billing_city( 'West Bromwich' );
    $customer->set_billing_postcode( 'B71 4LF' );
    $customer->set_billing_country( 'GB' );
    $customer->set_billing_phone( '0000 000 0000' );
    
    // Save customer's metadata to the WC Customer instance.
    $customer->save();
    

    Your customers details will show in the Users tab of the WP Admin and in their account details.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search