<p>Just realized that there is another way. No need for a custom plugin and shouldn’t be too heavy on the performance side of things.</p>

<pre><code>{# Query for licenses, eager-load owner #}
{% set licenses = craft.digitalProducts.licenses.with(['owner']) %}

{# Collect owner ids #}
{% set licensedUserIds = [] %}

{% if licenses %}
    {% for license in licenses if license.userId %}
        {% set licensedUserIds = licensedUserIds|merge([license.userId]) %}
    {% endfor %}
{% endif %}

{# Query for licensed users #}
{% set licensedUsers = craft.users({
    group: 'profiles',
    id: licensedUserIds
}) %}
</code></pre>

CC BY-SA 3.0
2017-01-30T10:25:55.987Z
2017-01-30T10:25:55.987Z
medoingthings
<p>I don't think it's currently possible in as clean as manner as you'd like. I'd tackle the problem by writing a small plugin that queries the database and returns a list of user ids that match the criteria.</p>

<p>Remember, though, is that a license does not necessarily have a user id attached to it - a license must have either a user id or an email assigned to, so you might want to keep that in mind.</p>

CC BY-SA 3.0
2017-01-30T09:14:02.777Z
Thanks for pointing me in that direction. I had hoped that there might be a way already.
medoingthings
CC BY-SA 3.0
2017-01-30T07:25:59.287Z
2017-01-30T07:25:59.287Z
Andris Sevcenko
<p>I’m trying to use Craft Commerce with the Digital Products plugin for a site where users can pay for being listed on that site.</p>

<p>Therefore I need to query for all users with certain criterias plus a valid license.</p>

<p>I’m not sure how to tackle this. I tried something like this, but it doesn’t work.</p>

<pre><code>{% for user in craft.users({
    group: 'profiles',
    relatedTo: craft.digitalProducts.products({type: 'access'})
}) %}
</code></pre>

<p>Another approach would be to list all licenses, collect the related users and then list them. But that doesn’t seem like a good idea either, in terms of performance.</p>

<p>Any hint is much appreciated. </p>

<plugin-craftcommerce><plugin-digitalproducts>
How do I query for all users that bought a license?
# How do I query for all users that bought a license?

(Asked by: username_0 on 2017-01-27)

I’m trying to use Craft Commerce with the Digital Products plugin for a site where users can pay for being listed on that site.
Therefore I need to query for all users with certain criterias plus a valid license.
I’m not sure how to tackle this. I tried something like this, but it doesn’t work.
{% for user in craft.users({
    group: 'profiles',
    relatedTo: craft.digitalProducts.products({type: 'access'})
}) %}

Another approach would be to list all licenses, collect the related users and then list them. But that doesn’t seem like a good idea either, in terms of performance.
Any hint is much appreciated.

**Tags:** plugin-craftcommerce, plugin-digitalproducts


---

## 2 answers

### Answer by username_0 on 2017-01-30. Score: 2 | Accepted answer

Just realized that there is another way. No need for a custom plugin and shouldn’t be too heavy on the performance side of things.
{# Query for licenses, eager-load owner #}
{% set licenses = craft.digitalProducts.licenses.with(['owner']) %}

{# Collect owner ids #}
{% set licensedUserIds = [] %}

{% if licenses %}
    {% for license in licenses if license.userId %}
        {% set licensedUserIds = licensedUserIds|merge([license.userId]) %}
    {% endfor %}
{% endif %}

{# Query for licensed users #}
{% set licensedUsers = craft.users({
    group: 'profiles',
    id: licensedUserIds
}) %}


---

### Answer by username_1 on 2017-01-30. Score: 4

I don't think it's currently possible in as clean as manner as you'd like. I'd tackle the problem by writing a small plugin that queries the database and returns a list of user ids that match the criteria.

Remember, though, is that a license does not necessarily have a user id attached to it - a license must have either a user id or an email assigned to, so you might want to keep that in mind.

> **Comments:**
> **username_0**: Thanks for pointing me in that direction. I had hoped that there might be a way already.  

---

