<p>Remembering a) that not all customers are users and b) that a user can be in one, or more, groups.....then something like this can be used to check if a user is in a particular group:</p>
<pre class="lang-php prettyprint-override"><code>            $order = $lineItem-&gt;getOrder();
            $user = Craft::$app-&gt;users-&gt;getUserByUsernameOrEmail($order-&gt;email);
            if ($user){
                foreach ($user-&gt;getGroups() as $group) {
                    if ($group-&gt;id == someParticularValue){
                        //do stuff
                    }
                }
            }
</code></pre>

CC BY-SA 4.0
2021-01-19T11:31:14.557Z
Thanks Jermey for helping me on the right track! This is exactly what i needed.
R14
CC BY-SA 4.0
2021-01-18T21:54:57.287Z
2021-01-18T21:54:57.287Z
Jeremy Daalder
<p>I have the following code working. The only thing i have to do is to check which usergroup is the customer in. Is there a simple way to check what usergroup is the customer in? This is my first custom module.</p>
<pre><code>Event::on(
LineItems::class,
LineItems::EVENT_POPULATE_LINE_ITEM,
function(LineItemEvent $event) {

    // @var LineItem $lineItem
    $lineItem = $event-&gt;lineItem;
    
    // @var bool $isNew
    $isNew = $event-&gt;isNew;
    
    $purchasable = $lineItem-&gt;getPurchasable();
    $variant = $purchasable-&gt;getProduct();

    // IF GUEST USER  &gt; NORMAL PRICE
    $lineItem-&gt;salePrice = $lineItem-&gt;getSalePrice();
    $lineItem-&gt;price = $lineItem-&gt;getPrice();

    // IF USER IS IN DEALER USERGROUP &gt; DEALER PRICE
    $lineItem-&gt;salePrice = $variant-&gt;dealerPrice;
    $lineItem-&gt;price = $variant-&gt;dealerPrice;
    
}
</code></pre>
<p>);</p>

<craft3><plugin-craftcommerce><modules><plugin-craftcommerce3>
Craft commerce 3 - Module - Lineitem - Get usergroup
# Craft commerce 3 - Module - Lineitem - Get usergroup

(Asked by: username_0 on 2021-01-18)

I have the following code working. The only thing i have to do is to check which usergroup is the customer in. Is there a simple way to check what usergroup is the customer in? This is my first custom module.
Event::on(
LineItems::class,
LineItems::EVENT_POPULATE_LINE_ITEM,
function(LineItemEvent $event) {

    // @var LineItem $lineItem
    $lineItem = $event->lineItem;
    
    // @var bool $isNew
    $isNew = $event->isNew;
    
    $purchasable = $lineItem->getPurchasable();
    $variant = $purchasable->getProduct();

    // IF GUEST USER  > NORMAL PRICE
    $lineItem->salePrice = $lineItem->getSalePrice();
    $lineItem->price = $lineItem->getPrice();

    // IF USER IS IN DEALER USERGROUP > DEALER PRICE
    $lineItem->salePrice = $variant->dealerPrice;
    $lineItem->price = $variant->dealerPrice;
    
}

);

**Tags:** craft3, plugin-craftcommerce, modules, plugin-craftcommerce3

---

## 1 answers

### Answer by username_1 on 2021-01-18. Score: 2 | Accepted answer

Remembering a) that not all customers are users and b) that a user can be in one, or more, groups.....then something like this can be used to check if a user is in a particular group:
            $order = $lineItem->getOrder();
            $user = Craft::$app->users->getUserByUsernameOrEmail($order->email);
            if ($user){
                foreach ($user->getGroups() as $group) {
                    if ($group->id == someParticularValue){
                        //do stuff
                    }
                }
            }

> **Comments:**
> **username_0**: Thanks Jermey for helping me on the right track! This is exactly what i needed.  

---

