verbb / wishlist

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

Null list returned if user.id is 1 (and not a currentUser) #116

Closed vonscriptor closed 1 year ago

vonscriptor commented 1 year ago

Describe the bug

Example: {% set followingList = craft.wishlist.lists().userId(user.id).type('following').one() %}

If the user.id is 1 it will return null even though that user has that list.

This doesn't seem to happen when you're a currentUser (signed in).

There is additional odd behavior when you logout.

Steps to reproduce

  1. Have a user with a user.id of 1
  2. Use the above list query example given that user 1 has that list
  3. Test as a currentUser and not to see conflicting values

Craft CMS version

4.3.1

Plugin version

2.0.3

Multi-site?

No

Additional context

I'm not sure how Craft CMS decides user ids, but my admin account is 1, then I have groups in 600's, 1500's, etc.

engram-design commented 1 year ago

That would be because craft.wishlist.lists() by default adds a query parameter to fetch lists for the current user, whether you specify a user or not. This is for ease-of-development, because most of the time you want to get the list of the currently logged-in user. It would be in fact dangerous to do otherwise (showing other users' lists), so it's there as a guard.

Changing to:

{% set followingList = craft.wishlist.lists(false).userId(user.id).type('following').one() %}

Should do the trick, if you want to force a user ID, but you also shouldn't have to do that. It will do this automatically: https://github.com/verbb/wishlist/blob/9078b32528e7c49d3c6a93a974fab4deb95321fe/src/variables/WishlistVariable.php#L33

Which sorts out what the current user is. In addition, this also handles getting the session-based list for guests, where a user ID doesn't exist.

See docs

vonscriptor commented 1 year ago

Gosh that was simple—sorry I overlooked it. Thank you!