Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/wet-dingos-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@shopify/hydrogen': minor
---

Updated Cart queries in two ways, one of which requires you to be using Storefront API `2022-07`:

1. [`CartLine`](https://shopify.dev/api/storefront/2022-04/objects/CartLine#fields) now uses [`CartLineEstimatedCost`'s `totalAmount`](https://shopify.dev/api/storefront/2022-04/objects/CartLineEstimatedCost) field for calculating the Line's total, instead of doing it manually.
2. Cart now uses [`totalQuantiy`](https://shopify.dev/api/storefront/2022-07/objects/Cart#field-cart-totalquantity) for calculating how many items are in the cart, instead of doing this manually. **Note that this feature is only available in Storefront API `2022-07` and newer.**
2 changes: 1 addition & 1 deletion packages/hydrogen/graphql.schema.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ export function CartLinePrice<TTag extends keyof JSX.IntrinsicElements>(
const cartLine = useCartLine();
const {priceType = 'regular', ...passthroughProps} = props;

const price =
const moneyV2 =
priceType === 'regular'
? cartLine.merchandise.priceV2
: cartLine.merchandise.compareAtPriceV2;
? cartLine.estimatedCost.totalAmount
: cartLine.estimatedCost.compareAtAmount;

if (price == null) {
if (moneyV2 == null) {
return null;
}

return (
<Money<TTag>
{...passthroughProps}
data={{
amount: `${parseFloat(price.amount) * cartLine.quantity}`,
currencyCode: price.currencyCode,
amount: `${moneyV2.amount}`,
currencyCode: moneyV2.currencyCode,
}}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ describe('<CartLinePrice />', () => {
it('renders <Money /> with the regular price by default', () => {
const line = {
...CART_LINE,
merchandise: {
...CART_LINE.merchandise,
priceV2: {
estimatedCost: {
...CART_LINE.estimatedCost,
totalAmount: {
amount: '50',
currencyCode: CurrencyCode.Usd,
},
Expand All @@ -33,9 +33,9 @@ describe('<CartLinePrice />', () => {
it('renders <Money /> with the compareAt price when `priceType` is `compareAt`', () => {
const line = {
...CART_LINE,
merchandise: {
...CART_LINE.merchandise,
compareAtPriceV2: {
estimatedCost: {
...CART_LINE.estimatedCost,
compareAtAmount: {
amount: '60',
currencyCode: CurrencyCode.Usd,
},
Expand All @@ -53,30 +53,6 @@ describe('<CartLinePrice />', () => {
});
});

it('factors in quantity for the amount passed to <Money />', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

No longer needed since we don't manually calculate this anymore.

const line = {
...CART_LINE,
quantity: 2,
merchandise: {
...CART_LINE.merchandise,
priceV2: {
amount: '50',
currencyCode: CurrencyCode.Usd,
},
},
};

const wrapper = mountWithProviders(
<CartLineProvider line={line}>
<CartLinePrice />
</CartLineProvider>
);

expect(wrapper).toContainReactComponent(Money, {
data: {amount: '100', currencyCode: CurrencyCode.Usd},
});
});

it('allows passthrough props', () => {
const line = {
...CART_LINE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,14 @@ export const CART_LINE = {
selectedOptions: [{name: 'size', value: 'large'}],
title: 'Product Name - Large',
},
estimatedCost: {
totalAmount: {
amount: '123',
currencyCode: CurrencyCode.Usd,
},
compareAtAmount: {
amount: '125',
currencyCode: CurrencyCode.Usd,
},
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -680,12 +680,7 @@ export function CartProvider({
}),
status: state.status,
error: 'error' in state ? state.error : undefined,
totalQuantity:
'cart' in state
? state.cart.lines.reduce((previous, current) => {
return previous + current.quantity;
}, 0)
: 0,
totalQuantity: 'cart' in state ? state?.cart?.totalQuantity ?? 0 : 0,
cartCreate,
linesAdd(lines: CartLineInput[]) {
if ('cart' in state && state.cart.id) {
Expand Down
99 changes: 99 additions & 0 deletions packages/hydrogen/src/components/CartProvider/cart-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mutation CartLineAdd($cartId: ID!, $lines: [CartLineInput!]!, $numCartLines: Int
fragment CartFragment on Cart {
id
checkoutUrl
totalQuantity
buyerIdentity {
countryCode
customer {
Expand All @@ -31,6 +32,16 @@ fragment CartFragment on Cart {
key
value
}
estimatedCost {
totalAmount {
amount
currencyCode
}
compareAtAmount {
amount
currencyCode
}
}
merchandise {
... on ProductVariant {
id
Expand Down Expand Up @@ -108,6 +119,7 @@ mutation CartCreate($input: CartInput!, $numCartLines: Int = 250, $country: Coun
fragment CartFragment on Cart {
id
checkoutUrl
totalQuantity
buyerIdentity {
countryCode
customer {
Expand All @@ -129,6 +141,16 @@ fragment CartFragment on Cart {
key
value
}
estimatedCost {
totalAmount {
amount
currencyCode
}
compareAtAmount {
amount
currencyCode
}
}
merchandise {
... on ProductVariant {
id
Expand Down Expand Up @@ -206,6 +228,7 @@ mutation CartLineRemove($cartId: ID!, $lines: [ID!]!, $numCartLines: Int = 250,
fragment CartFragment on Cart {
id
checkoutUrl
totalQuantity
buyerIdentity {
countryCode
customer {
Expand All @@ -227,6 +250,16 @@ fragment CartFragment on Cart {
key
value
}
estimatedCost {
totalAmount {
amount
currencyCode
}
compareAtAmount {
amount
currencyCode
}
}
merchandise {
... on ProductVariant {
id
Expand Down Expand Up @@ -304,6 +337,7 @@ mutation CartLineUpdate($cartId: ID!, $lines: [CartLineUpdateInput!]!, $numCartL
fragment CartFragment on Cart {
id
checkoutUrl
totalQuantity
buyerIdentity {
countryCode
customer {
Expand All @@ -325,6 +359,16 @@ fragment CartFragment on Cart {
key
value
}
estimatedCost {
totalAmount {
amount
currencyCode
}
compareAtAmount {
amount
currencyCode
}
}
merchandise {
... on ProductVariant {
id
Expand Down Expand Up @@ -402,6 +446,7 @@ mutation CartNoteUpdate($cartId: ID!, $note: String, $numCartLines: Int = 250, $
fragment CartFragment on Cart {
id
checkoutUrl
totalQuantity
buyerIdentity {
countryCode
customer {
Expand All @@ -423,6 +468,16 @@ fragment CartFragment on Cart {
key
value
}
estimatedCost {
totalAmount {
amount
currencyCode
}
compareAtAmount {
amount
currencyCode
}
}
merchandise {
... on ProductVariant {
id
Expand Down Expand Up @@ -505,6 +560,7 @@ mutation CartBuyerIdentityUpdate(
fragment CartFragment on Cart {
id
checkoutUrl
totalQuantity
buyerIdentity {
countryCode
customer {
Expand All @@ -526,6 +582,16 @@ fragment CartFragment on Cart {
key
value
}
estimatedCost {
totalAmount {
amount
currencyCode
}
compareAtAmount {
amount
currencyCode
}
}
merchandise {
... on ProductVariant {
id
Expand Down Expand Up @@ -603,6 +669,7 @@ mutation CartAttributesUpdate($attributes: [AttributeInput!]!, $cartId: ID!, $nu
fragment CartFragment on Cart {
id
checkoutUrl
totalQuantity
buyerIdentity {
countryCode
customer {
Expand All @@ -624,6 +691,16 @@ fragment CartFragment on Cart {
key
value
}
estimatedCost {
totalAmount {
amount
currencyCode
}
compareAtAmount {
amount
currencyCode
}
}
merchandise {
... on ProductVariant {
id
Expand Down Expand Up @@ -701,6 +778,7 @@ mutation CartDiscountCodesUpdate($cartId: ID!, $discountCodes: [String!], $numCa
fragment CartFragment on Cart {
id
checkoutUrl
totalQuantity
buyerIdentity {
countryCode
customer {
Expand All @@ -722,6 +800,16 @@ fragment CartFragment on Cart {
key
value
}
estimatedCost {
totalAmount {
amount
currencyCode
}
compareAtAmount {
amount
currencyCode
}
}
merchandise {
... on ProductVariant {
id
Expand Down Expand Up @@ -797,6 +885,7 @@ query CartQuery($id: ID!, $numCartLines: Int = 250, $country: CountryCode = ZZ)
fragment CartFragment on Cart {
id
checkoutUrl
totalQuantity
buyerIdentity {
countryCode
customer {
Expand All @@ -818,6 +907,16 @@ fragment CartFragment on Cart {
key
value
}
estimatedCost {
totalAmount {
amount
currencyCode
}
compareAtAmount {
amount
currencyCode
}
}
merchandise {
... on ProductVariant {
id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type CartAttributesUpdateMutation = {__typename?: 'Mutation'} & {
cart?: Types.Maybe<
{__typename?: 'Cart'} & Pick<
Types.Cart,
'id' | 'checkoutUrl' | 'note'
'id' | 'checkoutUrl' | 'totalQuantity' | 'note'
> & {
buyerIdentity: {__typename?: 'CartBuyerIdentity'} & Pick<
Types.CartBuyerIdentity,
Expand All @@ -44,6 +44,16 @@ export type CartAttributesUpdateMutation = {__typename?: 'Mutation'} & {
'key' | 'value'
>
>;
estimatedCost: {__typename?: 'CartLineEstimatedCost'} & {
totalAmount: {__typename?: 'MoneyV2'} & Pick<
Types.MoneyV2,
'amount' | 'currencyCode'
>;
compareAtAmount: {__typename?: 'MoneyV2'} & Pick<
Types.MoneyV2,
'amount' | 'currencyCode'
>;
};
merchandise: {__typename?: 'ProductVariant'} & Pick<
Types.ProductVariant,
'id' | 'availableForSale' | 'requiresShipping' | 'title'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type CartBuyerIdentityUpdateMutation = {__typename?: 'Mutation'} & {
cart?: Types.Maybe<
{__typename?: 'Cart'} & Pick<
Types.Cart,
'id' | 'checkoutUrl' | 'note'
'id' | 'checkoutUrl' | 'totalQuantity' | 'note'
> & {
buyerIdentity: {__typename?: 'CartBuyerIdentity'} & Pick<
Types.CartBuyerIdentity,
Expand All @@ -44,6 +44,16 @@ export type CartBuyerIdentityUpdateMutation = {__typename?: 'Mutation'} & {
'key' | 'value'
>
>;
estimatedCost: {__typename?: 'CartLineEstimatedCost'} & {
totalAmount: {__typename?: 'MoneyV2'} & Pick<
Types.MoneyV2,
'amount' | 'currencyCode'
>;
compareAtAmount: {__typename?: 'MoneyV2'} & Pick<
Types.MoneyV2,
'amount' | 'currencyCode'
>;
};
merchandise: {__typename?: 'ProductVariant'} & Pick<
Types.ProductVariant,
'id' | 'availableForSale' | 'requiresShipping' | 'title'
Expand Down
Loading