Last active
July 27, 2018 18:56
-
-
Save rgoytacaz/12e0fd6db79fedb7433ed8d2d5ffdf24 to your computer and use it in GitHub Desktop.
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
//Depends on JQUERY | |
//Depends on CL Entity's rclastcart field to be public to read | |
(function(accountName,identityExpiration) { | |
function setOrRenewUserEmailToSession(email) { | |
if(email && email != "") { | |
if(localStorage) { localStorage.setItem("vtex-user-email", email) }; | |
if(vtex && vtex.NavigationCapture) { | |
//Saves user cart, for abbandoned cart. | |
vtex.NavigationCapture.sendEvent('userContactInfo', { visitorContactInfo: [email] }); | |
} | |
} | |
} | |
function getLastCart(email) { | |
$.ajax({ | |
contentType: 'application/json', | |
type: 'GET', | |
url: "//api.vtexcrm.com.br/" + accountName + "/dataentities/CL/search?_where=email=" + email + "&_fields=rclastcart", | |
}).done(function(result) { | |
if(result && result[0]) { restoreCart(result[0]['rclastcart']) } | |
}); | |
} | |
function restoreCart(cart) { | |
if(cart == null) return; | |
var cartInfo = (cart.split("?")[1]).split("&"); | |
var items = []; | |
var i = 0; | |
if(cartInfo.length < 3) return; | |
//TODO prevent multiples runs per session | |
while(i < (cartInfo.length - 1)) { | |
var h=i*1; var j=(i*1)+1; var k=(i*1)+2; | |
var item = { | |
id: cartInfo[h].split("=")[1], | |
quantity: cartInfo[j].split("=")[1], | |
seller: cartInfo[k].split("=")[1] | |
}; | |
items.push(item); | |
i=i+3; | |
} | |
vtexjs.checkout.addToCart(items); | |
} | |
$(window).on('orderFormUpdated.vtex', function(ev, orderForm) { | |
if(orderForm && orderForm.clientProfileData && orderForm.clientProfileData.email) { | |
setOrRenewUserEmailToSession(orderForm.clientProfileData.email); | |
} | |
//saves cart to masterdata on update | |
}); | |
$(function() { | |
if(localStorage && localStorage.getItem("vtex-user-email") != null) { | |
setOrRenewUserEmailToSession(localStorage.getItem("vtex-user-email")); | |
getLastCart(localStorage.getItem("vtex-user-email")); | |
} | |
}); | |
})("accountName",90);//TODO define accountName |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment