Update buyer identity
Cart buyer identity is used for associating customer information with a cart. You should update cart buyer identity when customer information is available:
-
Update
countryCodewhen a customer changes site currency or locale so the cart will return pricing in the correct currency. -
Update
customerAccessTokenwhen a customer logs into a customer account so the cart will return selected delivery options.This guide shows you how to use the cart handler to update buyer identity.
Anchor to RequirementsRequirements
- You've completed the quickstart guide.
- You've set up a cart handler.
Anchor to Handle the buyer identity updateHandle the buyer identity update
Handle the buyer identity update in an action. Use the cart, created from createCartHandler, to handle cart mutation requests to the Storefront API.
File
/app/routes/account.login.jsx
export async function action({request, context}) {
const {cart, session} = context;
const formData = await request.formData();
const email = formData.get('email');
const password = formData.get('password');
try {
// Obtain a customer access token
// https://shopify.dev/docs/custom-storefronts/building-with-the-storefront-api/customer-accounts#step-3-create-an-access-token
const customerAccessToken = await doLogin(context, {email, password});
session.set('customerAccessToken', customerAccessToken);
// Sync customerAccessToken with existing cart
const result = await cart.updateBuyerIdentity({customerAccessToken});
// Update cart id in cookie
const headers = cart.setCartId(result.cart.id);
// Update session
headers.append('Set-Cookie', await session.commit());
return redirect(params.locale ? `/${params.locale}/account` : '/account', {
headers,
});
} catch (error: any) {
// handle error
}
};
export async function action({request, context}: ActionArgs) {
const {cart, session} = context;
const formData = await request.formData();
const email = formData.get('email');
const password = formData.get('password');
try {
// Obtain a customer access token
// https://shopify.dev/docs/custom-storefronts/building-with-the-storefront-api/customer-accounts#step-3-create-an-access-token
const customerAccessToken = await doLogin(context, {email, password});
session.set('customerAccessToken', customerAccessToken);
// Sync customerAccessToken with existing cart
const result = await cart.updateBuyerIdentity({customerAccessToken});
// Update cart id in cookie
const headers = cart.setCartId(result.cart.id);
// Update session
headers.append('Set-Cookie', await session.commit());
return redirect(params.locale ? `/${params.locale}/account` : '/account', {
headers,
});
} catch (error: any) {
// handle error
}
};
JavaScript
export async function action({request, context}) {
const {cart, session} = context;
const formData = await request.formData();
const email = formData.get('email');
const password = formData.get('password');
try {
// Obtain a customer access token
// https://shopify.dev/docs/custom-storefronts/building-with-the-storefront-api/customer-accounts#step-3-create-an-access-token
const customerAccessToken = await doLogin(context, {email, password});
session.set('customerAccessToken', customerAccessToken);
// Sync customerAccessToken with existing cart
const result = await cart.updateBuyerIdentity({customerAccessToken});
// Update cart id in cookie
const headers = cart.setCartId(result.cart.id);
// Update session
headers.append('Set-Cookie', await session.commit());
return redirect(params.locale ? `/${params.locale}/account` : '/account', {
headers,
});
} catch (error: any) {
// handle error
}
};TypeScript
export async function action({request, context}: ActionArgs) {
const {cart, session} = context;
const formData = await request.formData();
const email = formData.get('email');
const password = formData.get('password');
try {
// Obtain a customer access token
// https://shopify.dev/docs/custom-storefronts/building-with-the-storefront-api/customer-accounts#step-3-create-an-access-token
const customerAccessToken = await doLogin(context, {email, password});
session.set('customerAccessToken', customerAccessToken);
// Sync customerAccessToken with existing cart
const result = await cart.updateBuyerIdentity({customerAccessToken});
// Update cart id in cookie
const headers = cart.setCartId(result.cart.id);
// Update session
headers.append('Set-Cookie', await session.commit());
return redirect(params.locale ? `/${params.locale}/account` : '/account', {
headers,
});
} catch (error: any) {
// handle error
}
};Anchor to Next stepsNext steps
- Learn how to apply cart notes.
Was this page helpful?