Using Snipcart.store.getState().customer on my site

Hello,
I am trying to use Snipcart.store.getState().customer to pull the current customer’s email address. But this always shows that there is no customer logged in. It’s not seeing the current customer and I’m not sure why.

<script>
document.addEventListener('snipcart.ready', () => {
const customer = Snipcart.store.getState().customer;
console.log(customer.email);
});
</script>

Hi @greyraven1, thanks for reaching out.

Your code is running after Snipcart is ready, but not before full initialization, which is needed to fetch logged user data.

Changing your code to

    <script>
      document.addEventListener("snipcart.ready", () => {
        Snipcart.events.on("snipcart.initialized", (snipcartState) => {
          const customer = Snipcart.store.getState().customer;
          console.log(customer.email);
        });
      });
    </script>

Should do the trick.

Let me know if you have further questions.

Thanks,