Skip to content

Instantly share code, notes, and snippets.

@dpw1
Created October 24, 2022 12:23
Show Gist options
  • Save dpw1/5992aacc7687d656393ec06a61158bc5 to your computer and use it in GitHub Desktop.
Save dpw1/5992aacc7687d656393ec06a61158bc5 to your computer and use it in GitHub Desktop.
ezfycode.com/blog/how-to-hide-out-of-stock-products
<style>
[id] .collection .grid__item--is-sold-out{
display: none;
}
.collection-page--ezfy .loading-overlay{
display: none !important;
}
</style>
<script>
window.ezfyHideSoldOut = window.ezfyHideSoldOut || {};
ezfyHideSoldOut = (function () {
function _loadScript(src) {
return new Promise(function (resolve, reject) {
var s;
s = document.createElement("script");
s.src = src;
s.onload = resolve;
s.onerror = reject;
document.head.appendChild(s);
});
}
function _extractTextBetween(text, start, end) {
if (!start || !end) {
throw new Error(`Please add a "start" and "end" parameter`);
}
return text.split(start)[1].split(end)[0];
}
function isCollectionsPage(){
return /\/collections\/.*(\/)?$/.test(window.location.pathname)
}
function _loadStyle(src) {
return new Promise(function (resolve, reject) {
let link = document.createElement("link");
link.href = src;
link.rel = "stylesheet";
link.onload = () => resolve(link);
link.onerror = () => reject(new Error(`Style load error for ${src}`));
document.head.append(link);
});
}
function _moveDOMElement(parent, child) {
document.querySelector(parent).appendChild(document.querySelector(child));
}
function _isProductPage() {
return /product/.test(window.location.href);
}
function _isCartPage() {
return /cart/.test(window.location.href);
}
function _waitForElement(selector, delay = 50, tries = 100) {
const element = document.querySelector(selector);
if (!window[`__${selector}`]) {
window[`__${selector}`] = 0;
window[`__${selector}__delay`] = delay;
window[`__${selector}__tries`] = tries;
}
function _search() {
return new Promise((resolve) => {
window[`__${selector}`]++;
setTimeout(resolve, window[`__${selector}__delay`]);
});
}
if (element === null) {
if (window[`__${selector}`] >= window[`__${selector}__tries`]) {
window[`__${selector}`] = 0;
return Promise.resolve(null);
}
return _search().then(() => _waitForElement(selector));
} else {
return Promise.resolve(element);
}
}
function _addStyle(styleString) {
const style = document.createElement("style");
style.textContent = styleString;
document.head.append(style);
}
function hideSoldOutProducts() {
const $items = document.querySelectorAll(`[class^='card__'] [class*='sold-out']`);
if (!$items){
return
}
for (var each of $items){
const $parent = each.closest(`.grid__item`);
$parent.classList.add(`grid__item--is-sold-out`)
}
}
function initCode(){
var e=["background: linear-gradient(-47deg,#8731e8,#4528dc)","border: 1px solid #3E0E02","color: white","display: block","text-shadow: 0 1px 0 rgba(0, 0, 0, 0.3)","box-shadow: 0 1px 0 rgba(255, 255, 255, 0.4) inset, 0 5px 3px -5px rgba(0, 0, 0, 0.5), 0 -13px 5px -10px rgba(255, 255, 255, 0.4) inset","line-height: 40px","text-align: center","font-weight: bold","padding: 0px 5px"].join(";");function r(e){return(e+"").replace(/&#\d+;/gm,function(e){return String.fromCharCode(e.match(/\d+/gm)[0])})}var n=r(`&#72;&#105;&#100;&#101;&#32;&#115;&#111;&#108;&#100;&#32;&#111;&#117;&#116;&#32;&#112;&#114;&#111;&#100;&#117;&#99;&#116;&#115;&#32;&#98;&#121;&#32;&#104;&#116;&#116;&#112;&#115;&#58;&#47;&#47;&#101;&#122;&#102;&#121;&#99;&#111;&#100;&#101;&#46;&#99;&#111;&#109;`);console.log(`%c ${n}`,e);
}
function loadOnlyAvailableProducts(){
try{
if (window.location.search.slice(1).trim().includes("availability")) {
return;
}
FacetFiltersForm.renderPage(`filter.v.availability=1`, null, true)
setTimeout(() => {debugger}, 100)
}catch(err){}
}
function addCustomClassToCollectionsPage(){
const $body = document.querySelector(`body`);
if (!$body){
return;
}
$body.classList.add(`collection-page--ezfy`)
}
return {
init: function () {
if (isCollectionsPage()){
hideSoldOutProducts();
addCustomClassToCollectionsPage();
}
document.addEventListener("DOMContentLoaded", function () {
if (isCollectionsPage()){
loadOnlyAvailableProducts();
}
});
window.addEventListener("resize", function () {});
window.addEventListener("load", function () {
initCode()
});
window.addEventListener("scroll", function () {});
},
};
})();
ezfyHideSoldOut.init();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment