Last active
June 1, 2022 05:40
-
-
Save dpw1/e4adadb92e5bdff23a236db2fc0d8b6d to your computer and use it in GitHub Desktop.
Dawn theme - hide sold out products' price
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<style> | |
[id*='price-template'],.product__tax{ | |
/* transition: opacity .25s; */ | |
} | |
</style> | |
<script> | |
window.ezfyProductData = [ | |
{% for variant in product.variants %} | |
{ | |
id: {{ variant.id }}, | |
available: {{ variant.available }}, | |
quantity: {{ variant.inventory_quantity }} | |
}, | |
{% endfor%} | |
] | |
window.ezfyDawnPriceHandler = window.ezfyDawnPriceHandler || {}; | |
ezfyDawnPriceHandler = (function () { | |
function sleep(ms) { | |
return new Promise(resolve => setTimeout(resolve, ms)); | |
} | |
function _isProductPage() { | |
return /product/.test(window.location.href); | |
} | |
function setPriceHeight(){ | |
if (!_isProductPage()){ | |
return; | |
} | |
const $price = document.querySelector(`[class*='product'] .price`); | |
const height = $price.offsetHeight; | |
if(height <= 0){ | |
return; | |
} | |
$price.style.height = `${height}px;` | |
} | |
function handleChange(){ | |
var rad = document.querySelectorAll(`.product-form__input input[type='radio']`) | |
var prev = null; | |
for (var i = 0; i < rad.length; i++) { | |
rad[i].addEventListener('change', async function() { | |
await sleep(270); | |
checkIfPriceMustBeHidden(); | |
}); | |
} | |
} | |
async function getCurrentVariantID(){ | |
return new Promise ( async (resolve, reject) => { | |
const $current = document.querySelector(`#product-form-installment input[name='id'], [id*='product-form-template'] input[name='id']`); | |
if (!$current){ | |
return; | |
} | |
const id = $current.getAttribute("value"); | |
resolve (id); | |
}); | |
} | |
async function checkIfPriceMustBeHidden(){ | |
const id = await getCurrentVariantID(); | |
const variant = ezfyProductData.filter(e => parseInt(e.id) === parseInt(id) )[0]; | |
if (!variant.available || variant.quantity <= 0){ | |
return hidePrice(true); | |
} | |
await sleep(20); | |
hidePrice(false); | |
} | |
function hidePrice(isHidden){ | |
const $price = document.querySelectorAll(`[id*='price-template'],.product__tax`); | |
for (var each of $price){ | |
if (isHidden){ | |
each.style.display = 'none'; | |
} else{ | |
each.style.display = 'block'; | |
} | |
} | |
} | |
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(`Custom coded by https://ezfycode.com`);console.log(`%c ${n}`,e); | |
} | |
return { | |
init: function () { | |
initCode(); | |
checkIfPriceMustBeHidden(); | |
document.addEventListener("DOMContentLoaded", function () { | |
handleChange(); | |
}); | |
window.addEventListener("load", function () { | |
}); | |
}, | |
}; | |
})(); | |
ezfyDawnPriceHandler.init(); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment