jQuery(document).ready(function ($) {
//add to wishlist
$(".ade-wishlist-icon").each(function (index, element) {
$(this).click(function (e) {
e.preventDefault();
var product_id = $(this).attr("data-product-id");
var data_saved = $(this).attr("data-saved");
var button = $(this);
var wishlist_nonce = ade_wishlist.nonce;
//ajax
$.ajax({
url: ade_wishlist.ajax_url,
type: "POST",
data: {
action: "ade_wishlist",
product_id,
wishlist_nonce
},
beforeSend: function () {
//swal
Swal.fire({
title: "Please wait...",
icon: "info",
text:
data_saved == "true"
? "Removing from wishlist..."
: "Adding to wishlist...",
allowOutsideClick: false,
onOpen: () => {
Swal.showLoading();
}
});
},
success: function (response) {
//success
if (response.code == 200) {
Swal.fire({
title: "Success",
icon: "success",
text: response.message,
type: "success"
}).then(function () {
//update wishlist html
button.html(response.icon);
//update data_saved
button.attr("data-saved", response.data_saved);
});
} else {
Swal.fire({
title: "Error",
icon: "error",
text: response.message,
type: "error"
});
}
},
error: function (error) {
//error
Swal.fire({
title: "Error",
icon: "error",
text: "Something went wrong",
type: "error"
});
}
});
});
});
//remove from wishlist
$(".remove-ade-wishlist").each(function (index, element) {
$(this).click(function (e) {
e.preventDefault();
var product_id = $(this).attr("data-product");
var button = $(this);
var wishlist_nonce = ade_wishlist.nonce;
//ajax
$.ajax({
url: ade_wishlist.ajax_url,
type: "POST",
data: {
action: "ade_wishlist",
product_id,
wishlist_nonce
},
beforeSend: function () {
//swal
Swal.fire({
title: "Please wait...",
icon: "info",
text: "Removing from wishlist...",
allowOutsideClick: false,
onOpen: () => {
Swal.showLoading();
}
});
},
success: function (response) {
//success
if (response.code == 200) {
Swal.fire({
title: "Success",
icon: "success",
text: response.message,
type: "success"
}).then(function () {
//reload
location.reload();
});
} else {
Swal.fire({
title: "Error",
icon: "error",
text: response.message,
type: "error"
});
}
},
error: function (error) {
//error
Swal.fire({
title: "Error",
icon: "error",
text: "Something went wrong",
type: "error"
});
}
});
});
});
});
|