Last active
May 20, 2020 06:59
-
-
Save drinkmorewaters/65aefa599fb5f3173348e4d9d7977256 to your computer and use it in GitHub Desktop.
Shopify savings calculator
This file contains 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
(function cartCheck() { | |
let fetchIt = fetch(`${window.location.origin}/cart.js`) | |
.then(response => response.json()) | |
.then((data) => { | |
const dataMap = data.items.map((d) => { | |
let handleURL = `${window.location.origin}/products/${d.handle}.js` | |
return handleURL | |
}) | |
for (let data of dataMap) { | |
fetch(data) | |
.then(response => response) | |
.then(data => data.json()) | |
.then((data) => { | |
let compare = data.compare_at_price / 100 | |
let price = data.price / 100 | |
if (compare != null && compare > 0) { | |
// Remove minus sign | |
let savings = Math.abs(price - compare) | |
let finalText = `You saved $${savings.toFixed(2)}!` | |
// Change this to suit your store | |
let finalSavings = document.querySelector('.drawer__footer') | |
let newDivGet = document.querySelector('.compare__price') | |
if (newDivGet == null) { | |
let newDiv = document.createElement('div') | |
newDiv.classList.add('compare__price') | |
let newP = document.createElement('p') | |
newP.innerHTML = `${finalText}` | |
newDiv.appendChild(newP) | |
newDiv.style = "display: flex; justify-content: center;" | |
finalSavings.appendChild(newDiv) | |
} | |
} | |
}) | |
.catch((e) => { | |
console.log(e) | |
}) | |
} | |
}) | |
.catch((e) => { | |
console.log(e) | |
}) | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment