Skip to content

Instantly share code, notes, and snippets.

@joshuaiz
Last active July 9, 2020 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshuaiz/75ecf6014c3f820b06aaf06b75e3d483 to your computer and use it in GitHub Desktop.
Save joshuaiz/75ecf6014c3f820b06aaf06b75e3d483 to your computer and use it in GitHub Desktop.
/* GC */
let gcDataArray = []
let gcCachedData = JSON.parse(localStorage.getItem('ma_pgc'))
// when customer adds physical gift card(s) to the cart
$('body').on('click', '.add-to-cart-physical', function () {
let notes = ''
console.log('atc pgc clicked')
gcCachedData = JSON.parse(localStorage.getItem('ma_pgc'))
console.log('gcCachedData', gcCachedData)
if (gcCachedData) {
gcDataArray = gcCachedData
}
// initialize gc array
let gcArray = []
// console.log('atc click', quantityValue)
// loop through quantity and push
for (let i = 0; i < quantityValue; i++) {
let gcData = {
denomination: selectedPrice,
quantity: 1,
sku: selectedSKU,
gc: makeid(16),
}
gcDataArray.push(gcData)
}
console.log('gcDataArray', gcDataArray)
gcArray = JSON.stringify(gcDataArray)
console.log('giftCardArray', gcArray)
localStorage.setItem('ma_pgc', gcArray)
// loop through data to create notes
for (let i = 0; i < gcDataArray.length; i++) {
let gcAmountCurrency = gcDataArray[i].denomination
let gcAmount = gcAmountCurrency.substr(0, gcAmountCurrency.indexOf('.'))
let gcAmountStripped = gcAmount.substr(1)
notes += gcAmountStripped + ':' + gcDataArray[i].gc + ','
}
console.log('notes', notes)
notes = notes.toString()
let gcObj = {
note: 'gc_' + notes,
}
// update cart with new data
$.post('/cart/update.js', gcObj, function (data) {
console.log('cart update data', data) // <-- this is not returning data. Why?
})
console.log('gcObj', JSON.stringify(gcObj))
})
function makeid(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment