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();
  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


It should be as simple as:


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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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