<script>
const items = [];
const shoppingCartItems = [];
window.addEventListener(
'message',
(event) => {
if (event.data && event.data.eventName === 'addShoppigCartItem') {
shoppingCartItems.push({ vendor_code: event.data.payload.item.vendor_code });
window.postMessage(
{
eventName: 'setShoppingCartStore',
payload: shoppingCartItems,
},
'*',
);
}
if (event.data && event.data.eventName === 'removeShoppigCartItem') {
// Event handling
const foundIndex = shoppingCartItems.findIndex(
(item) => item.vendor_code === event.data.payload.item.vendor_code,
);
console.log(foundIndex);
if (foundIndex !== -1) {
shoppingCartItems.splice(foundIndex, 1);
window.postMessage(
{
eventName: 'setShoppingCartStore',
payload: shoppingCartItems,
},
'*',
);
}
}
},
false,
);
</script> |