3

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.

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.

1
  • Thanks for pointing me in that direction. I had hoped that there might be a way already. – medoingthings Jan 30 '17 at 9:14
2

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
}) %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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