1

How can I get the user ID from an Ubercart order?

3
use Drupal\uc_order\Entity\Order;
$order_load = Order::load($order_id); //Load Order
$ownerinfo = $order_load->getOwner(); //Fetch Owner Info
$ownerID = $order_load->getOwnerId(); //Fetch Owner ID
$ownerEmail = $order_load->getEmail(); //Fetch owner email
| improve this answer | |
-2
    $connection = \Drupal::database();
$query = $connection->query("SELECT uc_orders.order_id AS order_id, users_field_data_uc_orders.uid AS users_field_data_uc_orders_uid FROM  {uc_orders} uc_orders LEFT JOIN {users_field_data} users_field_data_uc_orders ON uc_orders.uid = users_field_data_uc_orders.uid");
$result = $query->fetchAll();
foreach ($result as $record) { 
  if($record->users_field_data_uc_orders_uid != 0){ 
     $user = \Drupal\user\Entity\User::load($record->users_field_data_uc_orders_uid); // pass your uid
     $name = $user->getUsername();
     if ($name) {
        $user->addRole('attandee_role');
        $user->save();
     }
  }      
}
| improve this answer | |
  • Loading DB query for getting single info is not required. And that to your query will give results of all users who have orders. If you want user id for particular order check above answer. – Ajay Reddy Apr 9 '18 at 4:51
  • On top of that the user save has nothing to do with the question – Kevin Apr 9 '18 at 14:26

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