<p>Since the discount should apply to all customers <em>except</em> those in the specific user group, you only need to check if there is an account for the current customer. Either the user is already logged in, in which case you just use their account. Or they are not logged in, in which case you can check if an account for the email address used in the order exists.</p>
<pre class="lang-php prettyprint-override"><code>use craft\elements\User;

public function adjust(Order $order): array
{
    /** @var ?User $user */
    $user = Craft::$app-&gt;getUser()-&gt;getIdentity()
        ?? User::find()-&gt;email($order-&gt;email)-&gt;one();

    if ($user &amp;&amp; $user-&gt;isInGroup('no_discount_for_you')) {
        return [];
    }

    // apply the discount
}
</code></pre>
<p>If a user account exists <em>and</em> they are in the specified group, the adjuster returns an empty array (no adjustment is applied). If no account exists, or the account is not in the specified group, you apply the discounts.</p>
<p>Keep in mind that a discount that is applied for everyone <em>except</em> a specific group can be easily gained by logging out and using a different email address.</p>
<hr />
<p>Sidenote, don't use hardcoded IDs in your code. They are environment-specific, so if at any point another team-member spins up a local development environment for this site without a database dump, this code might break.</p>
<p>It's also bad for readability – what is <code>USER_GROUP_ID_2</code>? I prefer to use handles throughout my code for readability, makes it easier to understand the intent of the code.</p>

CC BY-SA 4.0
2023-12-19T12:42:57.043Z
Thanks Moritz! will try this out. Also for the advices. The user id name was just for this question, normally i have PRIVATE_USER_GROUP_ID and  RETAIL_USER_GROUP_ID
Henrik
CC BY-SA 4.0
2023-12-19T12:59:24.277Z
@Henrik Ah got it, in this case I would stick with those names for code examples here. Makes it easier to understand the intention of the code and what's not working correctly :) And I would still use something like `PRIVATE_USER_GROUP_HANDLE` instead of `_ID` to be safe.
MoritzLost
CC BY-SA 4.0
2023-12-19T10:00:44.943Z
2023-12-19T10:00:44.943Z
MoritzLost
<p>Using craft commerce 3.</p>
<p>I have an adjuster which applies a discount if a user of a certain usergroup.
I check this with:</p>
<pre><code>$user = Craft::$app-&gt;getUser()-&gt;getIdentity();
if (($user &amp;&amp; ($user-&gt;isInGroup(self::USER_GROUP_ID_1) {
...
}
</code></pre>
<p>Now i have the problem that i have orders which are made by &quot;only&quot; customers
(not user, at least i dont find them there)
and they dont get the discount applied. But they should get it.</p>
<p>For example there is one customer, which i dont find as a craft user, which made an order last year, where she got the discount.
Back then i made the discount via the backend and which it said, apply this discount for all but this User group (USER_GROUP_ID_2).</p>
<p>But it did not work out well performance wise, so i made the adjuster.
Apparently the disconts via the backend applied it to everyone (guest, customers, users)
but with the adjuster i can only check for users.</p>
<p>How can i check for customer, similiar i check for users?</p>

CC BY-SA 4.0
2023-12-19T08:38:00.880Z
What would you like to happen for a customer that doesn't have a user account? Only users have user groups, so if you have a discount that should only apply to a specific user group, it makes sense that customers don't get the discount. Do you want the previous behaviour back, where all customers/users _except_ those in `USER_GROUP_ID_2` get the discount?
MoritzLost
CC BY-SA 4.0
2023-12-19T08:38:39.893Z
If possible, you could upgrade to Commerce 4. All customers are users in Commerce 4, making checks like this one easier to program.
MoritzLost
CC BY-SA 4.0
2023-12-19T09:47:24.047Z
Everybody should get the discount, users, guest and customers, except users from this specific group. And I have this one user (but there are more, as there are over 2000 customers, but ~1500 users), which ordered before (getting the discount), but I cannot find her in the user's area. I don't know how this happens, probably the registration process, but I could not replicate it. 
Now she ordered again (with my adjuster) and didn't get the discount. So I want to catch these customers.
Henrik
<plugin-craftcommerce><users>
How do i check for customers, not users, in a cart
# How do i check for customers, not users, in a cart

(Asked by: username_0 on 2023-12-18)

Using craft commerce 3.
I have an adjuster which applies a discount if a user of a certain usergroup.
I check this with:
$user = Craft::$app->getUser()->getIdentity();
if (($user && ($user->isInGroup(self::USER_GROUP_ID_1) {
...
}

Now i have the problem that i have orders which are made by "only" customers
(not user, at least i dont find them there)
and they dont get the discount applied. But they should get it.
For example there is one customer, which i dont find as a craft user, which made an order last year, where she got the discount.
Back then i made the discount via the backend and which it said, apply this discount for all but this User group (USER_GROUP_ID_2).
But it did not work out well performance wise, so i made the adjuster.
Apparently the disconts via the backend applied it to everyone (guest, customers, users)
but with the adjuster i can only check for users.
How can i check for customer, similiar i check for users?

**Tags:** plugin-craftcommerce, users

> **Comments:**
> **username_1**: What would you like to happen for a customer that doesn't have a user account? Only users have user groups, so if you have a discount that should only apply to a specific user group, it makes sense that customers don't get the discount. Do you want the previous behaviour back, where all customers/users _except_ those in `USER_GROUP_ID_2` get the discount?  
> **username_1**: If possible, you could upgrade to Commerce 4. All customers are users in Commerce 4, making checks like this one easier to program.  
> **username_0**: Everybody should get the discount, users, guest and customers, except users from this specific group. And I have this one user (but there are more, as there are over 2000 customers, but ~1500 users), which ordered before (getting the discount), but I cannot find her in the user's area. I don't know how this happens, probably the registration process, but I could not replicate it.  Now she ordered again (with my adjuster) and didn't get the discount. So I want to catch these customers.  

---

## 1 answers

### Answer by username_1 on 2023-12-19. Score: 1 | Accepted answer

Since the discount should apply to all customers except those in the specific user group, you only need to check if there is an account for the current customer. Either the user is already logged in, in which case you just use their account. Or they are not logged in, in which case you can check if an account for the email address used in the order exists.
use craft\elements\User;

public function adjust(Order $order): array
{
    /** @var ?User $user */
    $user = Craft::$app->getUser()->getIdentity()
        ?? User::find()->email($order->email)->one();

    if ($user && $user->isInGroup('no_discount_for_you')) {
        return [];
    }

    // apply the discount
}

If a user account exists and they are in the specified group, the adjuster returns an empty array (no adjustment is applied). If no account exists, or the account is not in the specified group, you apply the discounts.
Keep in mind that a discount that is applied for everyone except a specific group can be easily gained by logging out and using a different email address.

Sidenote, don't use hardcoded IDs in your code. They are environment-specific, so if at any point another team-member spins up a local development environment for this site without a database dump, this code might break.
It's also bad for readability – what is USER_GROUP_ID_2? I prefer to use handles throughout my code for readability, makes it easier to understand the intent of the code.

> **Comments:**
> **username_0**: Thanks Moritz! will try this out. Also for the advices. The user id name was just for this question, normally i have PRIVATE_USER_GROUP_ID and  RETAIL_USER_GROUP_ID  
> **username_1**: @username_0 Ah got it, in this case I would stick with those names for code examples here. Makes it easier to understand the intention of the code and what's not working correctly :) And I would still use something like `PRIVATE_USER_GROUP_HANDLE` instead of `_ID` to be safe.  

---

