K
K
kentos2021-08-27 12:07:33
JavaScript
kentos, 2021-08-27 12:07:33

Why is the last element not removed?

Hello, I am writing a favorites plugin on WordPress and in connection with this question, I am processing deletions from the list, but when one product remains, it is deleted, but after a reboot, it reappears, here is the code:

$this
        .closest("ul")
        .find("li")
        .each(function () {
          wishlist.push($(this).data("product"));

          if (loggedIn) {
            // get user ID
            if (userData["user_id"]) {
              $.ajax({
                type: "POST",
                url: wish.ajaxPost,
                data: {
                  action: "user_wishlist_update",
                  user_id: userData["user_id"],
                  wishlist: wishlist.length !== 0 ? wishlist.join(",") : wishlist.toString(),
                },
              })
                .done(function (response) {
                  console.log(wishlist);
                  $this.closest("ul").removeClass("loading");
                  $this.closest("li").remove();
                  $(".wishlist_count").text(wishlist.length);
                })
                .fail(function (data) {
                  alert(wish.error);
                });
            }
          }


The request sends the correct data user: 1, wishlist: element id

A response to done is empty

and the action itself
function update_wishlist_ajax()
    {
        if (isset($_POST["user_id"]) && !empty($_POST["user_id"])) {
            $user_id   = $_POST["user_id"];
            $user_obj = get_user_by('id', $user_id);
            if (!is_wp_error($user_obj) && is_object($user_obj)) {
                update_user_meta($user_id, 'wishlist', $_POST["wishlist"]);
            }
        }
        die();
    }
    add_action('admin_post_nopriv_user_wishlist_update', 'update_wishlist_ajax');
    add_action('admin_post_user_wishlist_update', 'update_wishlist_ajax');

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question