我使用Woodmart主题,我会likne从愿望清单中删除产品第二次点击在withlist图标。基于woodmart主题,当您点击wishlist图标时,产品已添加到wishlist列表,之后当您第二次点击whishlist图标时,您可以看到whishlist页面并重定向到whishlist页面。因此,您只能在转到whishlis页面时将产品从愿望清单中删除。通过这种方式,我想从whishlist中删除产品,而无需将用户发送到whishlist页面,并在第二次单击时从wishlist中删除产品。任何帮助都很感激。我的代码到目前为止
jQuery(document).ready(function($) {
var clickCount = 0;
var clickTimeout;
$('.wd-wishlist-btn a').on('click', function(e) {
e.preventDefault();
var $this = $(this);
var productId = $this.data('product-id');
clickCount++;
if (clickCount === 1) {
clickTimeout = setTimeout(function() {
clickCount = 0;
addToWishlist(productId);
}, 300);
} else if (clickCount === 2) {
clearTimeout(clickTimeout);
clickCount = 0;
removeProductFromWishlist(productId);
}
});
function addToWishlist(productId) {
$.ajax({
url: woodmart_settings.ajaxurl,
data: {
action: 'woodmart_add_to_wishlist',
product_id: productId
},
dataType: 'json',
method: 'GET',
success: function(response) {
if (response) {
$('.wd-header-wishlist .wd-tools-count').text(response.count);
$('.wd-wishlist-btn a[data-product-id="' + productId + '"]').addClass('added');
} else {
console.log('Something went wrong while adding the product to the wishlist.');
}
},
error: function() {
console.log('Error: Unable to add the product to the wishlist.');
}
});
}
function removeProductFromWishlist(productId) {
$.ajax({
url: woodmart_settings.ajaxurl,
data: {
action: 'woodmart_remove_from_wishlist',
product_id: productId
},
dataType: 'json',
method: 'GET',
success: function(response) {
if (response) {
$('.wd-header-wishlist .wd-tools-count').text(response.count);
$('.wd-wishlist-btn a[data-product-id="' + productId + '"]').removeClass('added');
} else {
console.log('Something went wrong while removing the product from the wishlist.');
}
},
error: function() {
console.log('Error: Unable to remove the product from the wishlist.');
}
});
}
});
1条答案
按热度按时间r7xajy2e1#
你可以使用toggleClass来代替点击计数器,在第一次点击时你的类被添加(比如:选择),然后在第二次点击,如果被删除。
因此,您可以忽略计数器,并尝试这样做:
请确保您使用clickTimeout是否有特定原因。如果您可以只依赖于“选定”类,则也可以忽略这些