kylephillips / favorites

Simple and flexible favorite buttons for any WordPress post type.
https://favoriteposts.com
223 stars 86 forks source link

Is there a way to pragmatically clear an anonymous users favourites? #153

Open aaronbentley opened 3 years ago

aaronbentley commented 3 years ago

Hello, thanks for your work on this plugin, it's been super helpful for my latest project.

I am using this plugin to allow users to select favourites (anonymously), then submit their favourites via a form, I'd like to clear out the selected favourites once the form submission is complete.

Is there a way to do this pragmatically?

KR,

Aaron.

w3b-beweb commented 2 years ago

Same use case here. @AaronBentley have you found a way to prgrammatically delete all favourites for a user?

aaronbentley commented 2 years ago

Hey @w3b-beweb,

Apologies for the late reply, it's been a crazy few days in the world πŸ‡ΊπŸ‡¦.

I did find a way to accomplish this, it's not particularly pretty however it does accomplish what I was trying to do. Essentially I managed to mimic the behaviour of the built-in Clear Favourites button via custom javascript.

You need to have the built-in 'Clear Favourites' button on the page you wish to add this functionality to using this function call in your template:

the_clear_favorites_button();

Then simulate the click event, here's how I did it via jQuery:

// Get reference to the 'Clear favourites" button
const clearFavouritesButton = $( 'button.simplefavorites-clear' )

// Trigger an artificial click event to mimic the behaviour of a user clicking the Clear Favourites button
clearFavouritesButton.trigger( 'click' )

i imagine you would need to have this fire on a different event(such as a form submission event or similar).

If you want to see this implementation in practice:

In this instance, the custom clear favourites javascript is fired when a user successfully submits the 'Email Your Favourites' form.

Hope this helps my friend πŸ™Œ