verbb / wishlist

A Craft CMS plugin for wishlists for your users to save things to
Other
11 stars 12 forks source link

List all users orderBy number of wishlists #142

Open chrisrowe opened 3 months ago

chrisrowe commented 3 months ago

Question

I'm working on a dashboard for a client where they'd like to reach out to users with multiple wishlists and lots of saved elements. I can't think of a clean way to do a craft.users query but bring in wishlist count, I wondered if you had any ideas?

Additional context

No response

engram-design commented 3 months ago

What about something like this:

{% set users = craft.users.all() %}

{% for user in users %}
    {% set lists = craft.wishlist.lists(false, false).userId(user.id).all() %}

    Email: {{ user.email }} has {{ lists | length }} lists

    {% if lists | length > 1 %}
        {% set totalItems = 0 %}

        {% for list in lists %}
            {% set totalItems = totalItems + list.items | length %}
        {% endfor %}

        with {{ totalItems }} items
    {% endif %}

    <br>
{% endfor %}

I've added some text as a bit of a debug, but feel free to adjust to your needs. You'll need to loop through each user, get all their lists, check their list count, then check their total item count.

In terms of wanting to do a single query like "get me all users with X wishlists" that probably can't be done out of the box, other than some direct SQL in PHP/Twig.

chrisrowe commented 3 months ago

Thanks yeah I was hoping to go the single query route to be able to sort by number of wishlists and then futher query/paginate. I'll come up with something else with some PHP. Cheers