Skip to content

Instantly share code, notes, and snippets.

@mklasen
Created August 22, 2019 11:59
Show Gist options
  • Save mklasen/a0e0904e1e6278998016ae4be1eada2d to your computer and use it in GitHub Desktop.
Save mklasen/a0e0904e1e6278998016ae4be1eada2d to your computer and use it in GitHub Desktop.
Editor Tool Integration
<html>
<head>
<script type="text/javascript">
function addToCart() {
var data = [
{
product_id: 218,
customize_hash: 'erg5eyu2r245710',
quantity: 48
}
];
window.parent.postMessage(data, 'https://stroopwafels.test');
}
</script>
<style>
body {
background: #CCC;
padding: 50px;
}
</style>
</head>
<body>
<h1>Editor Tool</h1>
<a href="javascript:" onClick="addToCart()" style="background: blue; padding: 10px; color: white;">Add to cart</a>
</body>
</html>
window.addEventListener("message", receiveMessage, false);
function receiveMessage(event) {
event.data.map(function(item) {
console.log(item);
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://stroopwafels.test/?add-to-cart='+item.product_id+'&quantity='+item.quantity, true);
xhr.onload = function () {
console.log('loaded');
location.reload();
};
xhr.send();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment