This code is designed to store product details in a local storage variable whenever a user visits a product page, with the intention of remembering the last visited products. However, the variable currently only retains one product at a time instead of adding them with "|" separators. Any assistance in resolving this issue would be greatly appreciated. The code appears to be valid.
Best regards
events.domReady( function() {
var objLookedAt = {
'name' : document.getElementById('name').innerHTML.trim(),
'type' : document.getElementById('type').innerText.trim(),
'uri' : location.href,
'imgUri' : document.getElementById('productImg').src,
'price' : document.querySelectorAll('#prodPrice > span')[0].innerHTML.trim(),
'quantity' : document.querySelectorAll('#prodPrice > span')[1].innerHTML.trim()
},
cRead = window.localStorage.getItem('lvp'),
stringToSave = JSON.stringify( objLookedAt ),
obj, ind, currSave = [], seen = false;
if(cRead) {
currSave = cRead.split('|');
for(var i = 0; i < currSave.length; i++) {
obj = JSON.parse(currSave[i]);
if(objLookedAt.name === obj.name) {
seen = true;
ind = i;
}
}
}
if( seen ) {
currSave.splice(ind, 1);
}
currSave.reverse().push(stringToSave);
var cRev = currSave.reverse();
window.localStorage.setItem('lvp', cRev.slice(0, 4).join('|'));
});