Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Floating button;

  2. Entry point button.

Example

Code Block
languagejs
<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>