Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make <CartProvider> use country code from <ShopifyProvider> #2521

Merged
merged 3 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chilly-dogs-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/hydrogen': patch
---

Fix the `<CartProvider>` to by default pull localization from `<ShopifyProvider>`. You can still override the countryCode by passing a prop directly to `<CartProvider>`. Resolves https://github.com/Shopify/hydrogen/issues/622
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {CartNoteUpdateMutationVariables} from './graphql/CartNoteUpdateMutation.
import {useCartAPIStateMachine} from './useCartAPIStateMachine.client.js';
import {CART_ID_STORAGE_KEY} from './constants.js';
import {ClientAnalytics} from '../../foundation/Analytics/ClientAnalytics.js';
import {useLocalization} from '../../client.js';

export function CartProvider({
children,
Expand All @@ -57,7 +58,7 @@ export function CartProvider({
data: cart,
cartFragment = defaultCartFragment,
customerAccessToken,
countryCode = CountryCode.Us,
countryCode,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main thing is we don't default a countryCode in the prop, instead use the shopify provider hook to get the default country code.

}: {
/** Any `ReactNode` elements. */
children: React.ReactNode;
Expand Down Expand Up @@ -104,6 +105,12 @@ export function CartProvider({
/** The ISO country code for i18n. */
countryCode?: CountryCode;
}) {
const {country} = useLocalization();

countryCode = (
(countryCode as string) ?? country.isoCode
).toUpperCase() as CountryCode;

if (countryCode) countryCode = countryCode.toUpperCase() as CountryCode;
const [prevCountryCode, setPrevCountryCode] = useState(countryCode);
const [prevCustomerAccessToken, setPrevCustomerAccessToken] =
Expand Down