Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wishlist shows a completely different entry than the one originally saved #147

Open
knufri opened this issue Jan 30, 2025 · 8 comments
Open

Comments

@knufri
Copy link

knufri commented Jan 30, 2025

Describe the bug

I have a strange behaviour. I have entries of the auction type, which is content that expires after a certain time. I have a cronjob that deletes the expired entries via a custom module with

$success = Craft::$app->elements->deleteElement($entry);

I don't know if it's the deletion routine or what the cause is. The remembered entries work for a while and then the wishlist suddenly shows completely different entries with a different entry type!

This is how it looks in the cp:
Image

It should be an entry of the type auction but in the frontend it shows an complete wrong entry. If multiple items on the list it shoes the same wrong entry for each item:

Image

So far I have no idea what this could be due to. I have already deleted the list type and created a new one, but the error keeps recurring 😢

Steps to reproduce

  1. Create a wishlist type
  2. Save items on list
  3. After a random time there are wrong entries on the list

Craft CMS version

Craft CMS PRO 5.5.10

Plugin version

3.0.4

Multi-site?

No

Additional context

No response

@engram-design
Copy link
Member

When an element is deleted, the elementId of a Wishlist item is set to null. I wonder if you can check that in your database next time this happens? If you go to the edit page of the Wishlist Item, is the right-hand side "Element" control empty?

Can you also show what Twig templating you're using on the front-end to render your items?

@knufri
Copy link
Author

knufri commented Jan 31, 2025

Hi @engram-design,

I've checked the database for the "broken" wishlist an all elementId are set to NULL - see the screenshot attached:

Image

And when I understand your question correct the right-hand side "Element" control is empty but for the wrong item not:

Image

Here is my twig (sprig) code I use to show the wishlists in the frontend:

{% set auctionWishlist = craft.wishlist.getUserList({ listType: 'auctionWishlist' }) %}
{% set auctionWishlistItems = false %}
{% set limit = 10 %}
{% set page = page ?? 1 %}
{% set query = query ?? '' %}

{# Fetch the wishlist items with list type handle and current user id and prepare sprig pagination #}
{% if auctionWishlist.id %}
    {% set auctionWishlistItems = craft.wishlist.items()
        .listTypeHandle('auctionWishlist')
        .listId(auctionWishlist.id)
        .search(query)
        .limit(limit) %}

    {% set pageInfo = sprig.paginate(auctionWishlistItems, page) %}
    {% set results = pageInfo.pageResults %}
{% endif %}

{% if auctionWishlistItems %}
    <div class="@container/listing">
        <div id="auction-results">
            <ul>
                {% if results %}
                    {% for listItem in results %}
                        {# Query item from wishlist result #}
                        {% set item = craft.entries().id(listItem.elementId).one() ?? null %}

                        {% include '_partials/auctionTableRow.twig' with {
                            self: item,
                            context: 'wishlist'
                        } %}
                    {% endfor %}
                {% else %}
                    <li class="mt-8">
                        Die Suche ergab keine Treffer!
                    </li>
                {% endif %}
            </ul>

            {% if (auctionWishlistItems|length) > limit %}
                {% include '_sprig/includes/pagination' %}
            {% endif %}
        </div>
    </div>
{% else %}
    Noch keine Merkliste vorhanden!
{% endif %}

If you need more information let me know

@engram-design
Copy link
Member

engram-design commented Jan 31, 2025

So if you click on one of those wishlist item elements in that table, that was the panel I was interested in. But at least I believe things are looking correct with the elementId indeed being null.

The problem I see here is:

{% set item = craft.entries().id(listItem.elementId).one() ?? null %}

In this instance, listItem.elementId = null so you're calling {% set item = craft.entries().id(null).one() %} which will return the last-updated entry I believe. That's just a Craft native behaviour more than anything.

You'd be better off calling item.getElement().

@knufri
Copy link
Author

knufri commented Jan 31, 2025

OK, I'll look at my twig query but this explains not why suddenly saved elements get wrong on the wishlist 🤔 Here a screenshot from the cp clicked on a "wrong" wishlist item:

Image

So why a saved entry from type "auction" foo becomes to an entry type "bar"? Should an deleted entry not removed from the wishlist?

@engram-design
Copy link
Member

Thanks for confirming that edit screen in the control panel. So this Wishlist item absolutely has a elementId of null? It's just I can see the other items in the list not having any linked entries, so I wanted to double check.

Setting list items elementId to null on my end is working as I'd expect (the element picker field on the right is empty).

@knufri
Copy link
Author

knufri commented Jan 31, 2025

Hi @engram-design thanks for helping so far. But I'll try to describe the problem again. There are currently 6 items in the wishlist. 5 of them are also in the corresponding table in the database (see screenshot above). But where does the sixth incorrect entry (StefanStrüwind), which is definitely linked in the element picker, come from? There is no possibility in the frontend to add entries of this type to the wishlist. So I am still wondering how it can happen that completely wrong entries appear in the wishlist which are not even in the corresponding table?

Image Image

@engram-design
Copy link
Member

Thanks for clarifying. That's very odd indeed, and not something I can replicate on my end. Setting a wishlist item's elementId = null makes the "Element" be empty:

Image

Can you confirm the ID of that wishlist item Stefan Strüwind, check in the URL:

https://craft5.test/admin/wishlist/lists/wishlist/21445/items/21448

In the wishlist_items that respective ID, and the elementId.

I was just looking at your previous post:

Image

And there are 5 items with no elementId which seem to match your UI (the ones with a blank "Element column"). But your last screenshot, you've marked those as "IDs are in the db table", which is confusing, sorry!

@knufri
Copy link
Author

knufri commented Feb 1, 2025

Hi @engram-design,

I apologise for having expressed myself somewhat imprecisely. In fact, ‘Stefan Strüwind’ is present in the corresponding table with ID and elementID.

Image

I have removed the incorrect entry from the list and will monitor this and open a new issue if it occurs again. Many thanks for your help. Since I want to use the list queries with search and the query also returns items without a linked element, I have solved it in the frontend like this:

{% set auctionWishlistItems = craft.wishlist.items()
        .listTypeHandle('auctionWishlist')
        .listId(auctionWishlist.id)
        .elementId(':notempty:')
        .search(query)
        .limit(limit) %}

so my sprig pagination works correct and the problem you showed me with {% set item = craft.entries().id(null).one() %} is also solved.

You can close the issue for now 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants