Skip to content

Instantly share code, notes, and snippets.

@SubhamChoudhury2408
Created May 15, 2024 10:08
Show Gist options
  • Select an option

  • Save SubhamChoudhury2408/d716e917c8bf76b45f6d71498ab73a34 to your computer and use it in GitHub Desktop.

Select an option

Save SubhamChoudhury2408/d716e917c8bf76b45f6d71498ab73a34 to your computer and use it in GitHub Desktop.
Custom Script for enabling header counter for Multiple Lists Feature
<script defer>
function renderWishlistCounter(swat) {
var ogWishlistCount = swat.wishlistCount;
var ogFetchLists = swat.fetchLists;
var customFavCount;
var isCollectionsEnabled = swat.retailerSettings.Wishlist.EnableCollections;
var counter_elem = document.querySelectorAll(".swym-wishlist-header-counter");
swat.wishlistCount = function(renderFn) {
if (!isCollectionsEnabled) {
ogWishlistCount.apply(this, arguments);
} else {
if (typeof customFavCount === "undefined") {
ogFetchLists.apply(this, [{
callbackFn: function(lists) {
customFavCount = 0;
lists.forEach(function(l) {
customFavCount = customFavCount + l.listcontents.length;
});
renderFn(customFavCount);
},
errorFn: function(er) {
console.error(er);
}
}]);
} else {
renderFn(customFavCount);
}
}
}
if (isCollectionsEnabled) {
swat.fetchLists = function(opts) {
var ogCallbackFn = opts.callbackFn;
opts.callbackFn = function(lists) {
if (counter_elem) {
customFavCount = 0;
lists.forEach(function(l) {
customFavCount = customFavCount + l.listcontents.length;
});
updateCounter();
}
ogCallbackFn.apply(this, arguments);
}
ogFetchLists.apply(this, arguments);
}
}
function updateCounter() {
for (var i = 0; i < counter_elem.length; i++) {
if (window._swat.renderWishlistCount) {
window._swat.renderWishlistCount(counter_elem[i], function(count, element) {
console.log("updateCounter - debug - renderWishlistCount", count, element); /* Can be removed after verification */
});
}
}
}
let removeFromWLEvent = "sw:removedfromwishlist";
let countUpdateOnRemoval = function(e){
console.log("removed event encountered");
if (customFavCount !== undefined && customFavCount > 0) {
console.log("counter decremented");
customFavCount -= 1; // Decrement the count
updateCounter();
}
}
swat.evtLayer.addEventListener(removeFromWLEvent, countUpdateOnRemoval);
// Initial counter rendering
updateCounter();
}
if (!window.SwymCallbacks) {
window.SwymCallbacks = [];
}
window.SwymCallbacks.push(renderWishlistCounter);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment