3

I programmatically created a user using the following code.

use Drupal\user\Entity\User;

/**
 * Helper function to create a new user.
 */
function create_new_user() {
  $user = User::create();
  $user->setPassword('<password>');
  $user->enforceIsNew();
  $user->setEmail('<email>');
  $user->setUsername('<username>');
  $user->save();
  drupal_set_message("User with uid " . $user->id() . " saved!\n");
}

Now I need to set the ID of the created user into an order.

I've loaded the order using the following code.

$ord_id = 10;
$order = \Drupal::entityTypeManager()->getStorage('commerce_order')->load($ord_id);

I want to update the user ID of this order. How can I achieve this?

1 Answer 1

6

It should be as simple as:

$order->setCustomer($user)->save();

Or if you only have the user ID and don't want to load the account object:

$order->set('uid', $uid);
$order->save();
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.