verbb / wishlist

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

Add items in php #119

Closed JulieVelghe closed 1 year ago

JulieVelghe commented 1 year ago

Question

Hi, for a client, we have a formie form that has a radiolist with product, and a hidden field with a wishlist id. So when a user gets on the form, a wishlist should get created. What we would like to do, is when the form is submitted, to update this wishlist with the chosen product. Is there a possibility to do this via a custom module in php? At the moment I have the code to get the wishlist id and the corresponding wishlist list. $wishlistid = $submission->getFieldByHandle('wishlist'); // Create a new list query $myWishlist = \verbb\wishlist\elements\ListElement::find()->id($wishlistid)->one();

Is there a way/function for php to add items to a list?

Additional context

No response

engram-design commented 1 year ago

Yes, this could certainly be done in a custom module, and you're on the right track. Take a look at the add controller action which is used in Wishlist to add an item.

The bit you're looking for is:

$item = \verbb\wishlist\WishList::$plugin->getItems()->createItem($elementId, $listId, $listTypeId, true, $elementSiteId);

\verbb\wishlist\Wishlist::$plugin->getItems()->saveElement($item)

Take a look at the createItem method and most of those params are optional (list type, element site, etc). The only thing required is the elementId and listId (Wishlist List ID).

JulieVelghe commented 1 year ago

Aha, I think i've found it. I'm now able to add an item to a wishlist. Now for an extra step. Let's say, a user removes an item from the wishlist, ideally the submission that triggered this addition should be removed from the formie submissions. Is there a way to add my event submissions id ad hoc in my php code to the wishlist item?

at the moment I have the following code: $submfield = [ "submissionid" => $submission->id, ];

    $postItems = [
        [
            'elementId' => $productid,
            'elementSiteId' => $submissionsitedid,
            'options' => $submfield,
        ],
    ];

but I don't see the content of these 'options' in the cp.

JulieVelghe commented 1 year ago

small update on this: I can see these options in the database, so the data is being stored. But now I've noticed that I cannot remove items from a wishlist. When I try it via the CP, I can remove a wishlist item. I have written code to get the formie submission out of the options field, and to delete this in the event_before_delete. However, when I try to delete an item from the frontend side, nothing seems to happen, not even the event seems to get triggered. My modal seems to be correct:

<form method="post">
                    <input type="hidden" name="action" value="wishlist/items/remove">
                    <input type="hidden" name="elementId" value="172">
                    <input type="hidden" name="CRAFT_CSRF_TOKEN" value="uj1mYrrmvsw-0qEAEeCbHcXjPPiWZQG1X8nsB-LRTz9DEQ5tm7s6EthFVynC0sv6U4aQQUeO9GX1oBHO4QpR3DG4qj6uhydVLEVrJffYeUo=">
                    <div class="c-button-group">
                        <button class="c-button c-button--secondary" data-modal="#modal-172">
                            <span class="c-button__label">Cancel</span>
                        </button>
                        <button class="c-button" type="submit">
                            <span class="c-button__label">Remove product</span>
                        </button>
                    </div>
                </form>

Or is it because the elementId is a hidden field that I'm having problems?

engram-design commented 1 year ago

Yes, you can store item options if you like:

$item = \verbb\wishlist\WishList::$plugin->getItems()->createItem($elementId, $listId, $listTypeId, true, $elementSiteId);

$item->setOptions([
    'submissionId' => $submission->id,
]);

\verbb\wishlist\Wishlist::$plugin->getItems()->saveElement($item)

Again, this is all code that already exists in the controller action.

To remove the correct item, you'll also likely want to fetch the item that matches the same options data.

<input type="hidden" name="action" value="wishlist/items/remove">
<input type="hidden" name="options[submissionId]" value="1234">

By default, it will try to delete the item with an elementId of 172 but with null as the value for options - which there is no matching item (because you've got your submissionId stored in the options).