• Hi,

    I’m wondering if there is a way to update the favourites list if the ‘add to wishlist’ or ‘remove from wishlist’ button is clicked.

    At the moment I’m using the WP_query php loop found here to display favourites so it won’t update without changing the page or refreshing.

    Is there AJAX functionality I can make use of? I don’t have any experience with AJAX myself.

    Thanks

    https://www.ads-software.com/plugins/favorites/

Viewing 8 replies - 1 through 8 (of 8 total)
  • I’d be very interested in this functionality too.
    Using the same method for grabbing posts.

    Thanks!

    Thread Starter Benjosity

    (@benjosity)

    Hi Nickylew

    In the end I created a jQuery function that refreshed the page after the button had been clicked and the AJAX success event had gone through –

    $('.simplefavorite-button').click(function(event) {
        $(document).ajaxSuccess(function() {
            location.reload();
        });
    });

    It’s not exactly what I wanted but it’s all I could figure out without any experience with AJAX and it essentially solves the main issue of not having any PHP loops update after clicking the favourite button.

    Hi Benjosity

    That’s cool thanks for that!

    Hi there,

    The code below is ‘refreshing’ the ‘span.counter’ div, where I’ve added:
    <?php the_user_favorites_count($user_id, $site_id, $filters); ?>

    jQuery('body').on('click', 'button.simplefavorite-button', function() {
       jQuery("span.counter").load(location.href + " span.counter");
    });

    Sometimes it’s a little buggy, but it works for me!

    Regards,
    Benjamin

    Hi Benjosity, can you tell me where to put your code? I am struggling with the same problems you had and haven’t found a working solution until now.

    Thread Starter Benjosity

    (@benjosity)

    Hi jfvwork

    I’ve put it in my JavaScript/jQuery file where all my JavaScript and jQuery is.

    You could also place it in your header.php file like so –

    <script type="text/javascript">
    jQuery('.simplefavorite-button').click(function(event) {
        jQuery(document).ajaxSuccess(function() {
            location.reload();
        });
    });
    </script>

    I would reccomend you put it within your site’s js file though.

    Thank you Benjosity! I didnt expect an answer that fast!

    @benjamin-carlos

    That code is close to working but it won’t count the first add to favorite button you click or count the first one you remove.

    For example I have 3 add to favorite buttons on a page, I click the first button and the counter stays at 0, I click the 2nd button and the counter goes to 1 (but I’ve added 2 posts to my favorites). Once I’ve clicked all 3 buttons the counter says (2) unless page refreshed. Any idea why it’s not counting the first one?

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Dynamically update favourites list after favourite button clicked’ is closed to new replies.