<p>Since this is happening from the <code>onOrderComplete</code> event, the easiest way to get the currently logged in user is with <a href="https://craftcms.com/classreference/services/UserSessionService#getUser-detail" rel="nofollow">UserSessionService::getUser()</a>:</p>



<pre class="lang-php prettyprint-override"><code>$user = craft()-&gt;userSession-&gt;getUser();
</code></pre>

<p>Alternatively, if you want to go through the actual Commerce_OrderModel that was passed as a parameter on the event, you can do this:</p>

<pre class="lang-php prettyprint-override"><code>$user = $event-&gt;params['order']-&gt;getCustomer()-&gt;getUser();
</code></pre>

<p>Either way, it’s a good idea to verify that <code>$user</code> gets set to something, just in case:</p>

<pre class="lang-php prettyprint-override"><code>if ($user) {
    // ...
}
</code></pre>

CC BY-SA 3.0
2015-12-14T14:59:04.367Z
2015-12-14T14:59:04.367Z
Brandon Kelly
<p>I have a plugin that is listening to the <code>onOrderComplete</code> event within commerce. I am able to get the email address like this:</p>

<pre><code>craft()-&gt;on('commerce_orders.onOrderComplete', function (Event $event) {
    $event-&gt;params['order']-&gt;email;  // name@email.com

});
</code></pre>

<p>I can see the Id in the log, but since I'm in the order class, I'm not sure how to get to it. I can see <code>customerId</code>:</p>

<pre><code>$event-&gt;params['order']-&gt;customerId; // 3
</code></pre>

<p>But I've noticed the <code>customerId</code> and the <code>userId</code> are different. I need to be able to get the logged in <code>userId</code> vs. the <code>customerId</code></p>

<p>When I try getting just the id, I get the product id back (Which is what I would expect).</p>

<pre><code>$event-&gt;params['order']-&gt;id;  // 123
</code></pre>

<p>To relate to the twig side it would be something like this (obviously won't work):</p>

<pre><code>$event-&gt;params['order']['currentUser']-&gt;id;  // 12
</code></pre>

<p>Here is a part of the log: </p>

<pre><code>...
[_attributes:Craft\BaseModel:private] =&gt; Array
(
    [enabled] =&gt; 1
    [archived] =&gt; 0
    [locale] =&gt; en_us
    [localeEnabled] =&gt; 1
    [itemTotal] =&gt; 375
    [baseDiscount] =&gt; 0
    [baseShippingCost] =&gt; 0
    [totalPrice] =&gt; 375
    [totalPaid] =&gt; 375.00
    [billingAddressId] =&gt; 122
    [shippingAddressId] =&gt; 123
    [paymentMethodId] =&gt; 1
    [customerId] =&gt; 3
    [id] =&gt; 12   //  &lt;-- This guy right here
    [orderStatusId] =&gt; 1
...
</code></pre>

CC BY-SA 3.0
2015-12-11T02:13:45.007Z
Are all of your orders going to be with logged in users or do you have a guest checkout workflow?
Brad Bell
CC BY-SA 3.0
2015-12-11T02:49:37.037Z
They will always be logged in. You must have an account on my site in order to purchase a product.
Damon
CC BY-SA 3.0
2015-12-11T02:51:57.600Z
Unless I'm missing something isn't [currentUser](https://craftcms.com/docs/templating/global-variables#currentUser) what you're looking for?
Brad Bell
<plugin-development><plugin-craftcommerce>
How can I get the current user who purchased a product from the onOrderComplete event?
# How can I get the current user who purchased a product from the onOrderComplete event?

(Asked by: username_0 on 2015-12-10)

I have a plugin that is listening to the onOrderComplete event within commerce. I am able to get the email address like this:
craft()->on('commerce_orders.onOrderComplete', function (Event $event) {
    $event->params['order']->email;  // name@email.com

});

I can see the Id in the log, but since I'm in the order class, I'm not sure how to get to it. I can see customerId:
$event->params['order']->customerId; // 3

But I've noticed the customerId and the userId are different. I need to be able to get the logged in userId vs. the customerId
When I try getting just the id, I get the product id back (Which is what I would expect).
$event->params['order']->id;  // 123

To relate to the twig side it would be something like this (obviously won't work):
$event->params['order']['currentUser']->id;  // 12

Here is a part of the log: 
...
[_attributes:Craft\BaseModel:private] => Array
(
    [enabled] => 1
    [archived] => 0
    [locale] => en_us
    [localeEnabled] => 1
    [itemTotal] => 375
    [baseDiscount] => 0
    [baseShippingCost] => 0
    [totalPrice] => 375
    [totalPaid] => 375.00
    [billingAddressId] => 122
    [shippingAddressId] => 123
    [paymentMethodId] => 1
    [customerId] => 3
    [id] => 12   //  <-- This guy right here
    [orderStatusId] => 1
...

**Tags:** plugin-development, plugin-craftcommerce
> **Comments:**
> **username_1**: Are all of your orders going to be with logged in users or do you have a guest checkout workflow?  
> **username_0**: They will always be logged in. You must have an account on my site in order to purchase a product.  
> **username_1**: Unless I'm missing something isn't [currentUser](https://craftcms.com/docs/templating/global-variables#currentUser) what you're looking for?  

---

## 1 answers

### Answer by username_2 on 2015-12-14. Score: 4 | Accepted answer

Since this is happening from the onOrderComplete event, the easiest way to get the currently logged in user is with UserSessionService::getUser():
$user = craft()->userSession->getUser();

Alternatively, if you want to go through the actual Commerce_OrderModel that was passed as a parameter on the event, you can do this:
$user = $event->params['order']->getCustomer()->getUser();

Either way, it’s a good idea to verify that $user gets set to something, just in case:
if ($user) {
    // ...
}


---

