how to get customer email in checkout extensions

Solved

how to get customer email in checkout extensions

kojiro2hb
Shopify Partner
15 1 2

how to get customer email in checkout extensions ?

 

const { buyerIdentity: { customer }, selectedPaymentOptions } = useApi();
console.log("email");
console.log(customer.email);
i wrote this but console shows undefined
Accepted Solution (1)
Richard687
Shopify Partner
19 1 16

This is an accepted solution.

As far as I can tell, buyerIdentity.customer is only defined when the user is logged in - if that isn't the desired behaviour, you could use buyerIdentity.email instead.

Either way, you need to subscribe to the value. This is how I did it in React:

const { buyerIdentity } = useApi();
const
[email, setEmail] = useState(buyerIdentity.customer.current?.email);
useEffect(() => {
    buyerIdentity.customer.subscribe(value => setEmail(value.email));
}, [buyerIdentity]);
or
const { buyerIdentity } = useApi();
const
[email, setEmail] = useState(buyerIdentity.email.current);
useEffect(() => {
    buyerIdentity.email.subscribe(value => setEmail(value.email));
}, [buyerIdentity]);
Hope that helps!

View solution in original post

Replies 2 (2)

kojiro2hb
Shopify Partner
15 1 2

@Liam 

 

i would be appreciate if you could help me on this 🙏

Richard687
Shopify Partner
19 1 16

This is an accepted solution.

As far as I can tell, buyerIdentity.customer is only defined when the user is logged in - if that isn't the desired behaviour, you could use buyerIdentity.email instead.

Either way, you need to subscribe to the value. This is how I did it in React:

const { buyerIdentity } = useApi();
const
[email, setEmail] = useState(buyerIdentity.customer.current?.email);
useEffect(() => {
    buyerIdentity.customer.subscribe(value => setEmail(value.email));
}, [buyerIdentity]);
or
const { buyerIdentity } = useApi();
const
[email, setEmail] = useState(buyerIdentity.email.current);
useEffect(() => {
    buyerIdentity.email.subscribe(value => setEmail(value.email));
}, [buyerIdentity]);
Hope that helps!