diff --git a/framework/shopify/types.ts b/framework/shopify/types.ts index e7bcb2476..2f2176c35 100644 --- a/framework/shopify/types.ts +++ b/framework/shopify/types.ts @@ -1,5 +1,5 @@ import * as Core from '@commerce/types' -import { CheckoutLineItem } from './schema' +import { CheckoutLineItem, Maybe, Scalars } from './schema' export type ShopifyCheckout = { id: string @@ -18,6 +18,10 @@ export interface LineItem extends Core.LineItem { * Cart mutations */ +export type Product = Core.Product & { + totalInventory?: Maybe +} + export type OptionSelections = { option_id: number option_value: number | string diff --git a/framework/shopify/utils/checkout-to-cart.ts b/framework/shopify/utils/checkout-to-cart.ts index 034ff11d7..6e3fe2ba8 100644 --- a/framework/shopify/utils/checkout-to-cart.ts +++ b/framework/shopify/utils/checkout-to-cart.ts @@ -27,15 +27,8 @@ export type CheckoutPayload = | CheckoutQuery const checkoutToCart = (checkoutPayload?: Maybe): Cart => { - if (!checkoutPayload) { - throw new CommerceError({ - message: 'Missing checkout payload from response', - }) - } - - const checkout = checkoutPayload?.checkout throwUserErrors(checkoutPayload?.checkoutUserErrors) - + const checkout = checkoutPayload?.checkout if (!checkout) { throw new CommerceError({ message: 'Missing checkout object from response', diff --git a/framework/shopify/utils/normalize.ts b/framework/shopify/utils/normalize.ts index 25bdca053..246604dc3 100644 --- a/framework/shopify/utils/normalize.ts +++ b/framework/shopify/utils/normalize.ts @@ -1,5 +1,3 @@ -import { Product } from '@commerce/types' - import { Product as ShopifyProduct, Checkout, @@ -11,7 +9,7 @@ import { ProductOption, } from '../schema' -import type { Cart, LineItem } from '../types' +import type { Cart, LineItem, Product } from '../types' const money = ({ amount, currencyCode }: MoneyV2) => { return { @@ -86,6 +84,7 @@ export function normalizeProduct(productNode: ShopifyProduct): Product { handle, priceRange, options, + totalInventory, ...rest } = productNode @@ -99,6 +98,7 @@ export function normalizeProduct(productNode: ShopifyProduct): Product { price: money(priceRange?.minVariantPrice), images: normalizeProductImages(images), variants: variants ? normalizeProductVariants(variants) : [], + totalInventory, options: options ? options .filter((o) => o.name !== 'Title') // By default Shopify adds a 'Title' name when there's only one option. We don't need it. https://community.shopify.com/c/Shopify-APIs-SDKs/Adding-new-product-variant-is-automatically-adding-quot-Default/td-p/358095 diff --git a/framework/shopify/utils/queries/get-all-products-query.ts b/framework/shopify/utils/queries/get-all-products-query.ts index 5eb44c7a7..bcd05ee93 100644 --- a/framework/shopify/utils/queries/get-all-products-query.ts +++ b/framework/shopify/utils/queries/get-all-products-query.ts @@ -10,6 +10,7 @@ edges { vendor handle description + totalInventory priceRange { minVariantPrice { amount diff --git a/framework/shopify/utils/queries/get-product-query.ts b/framework/shopify/utils/queries/get-product-query.ts index 5c109901b..8386e23af 100644 --- a/framework/shopify/utils/queries/get-product-query.ts +++ b/framework/shopify/utils/queries/get-product-query.ts @@ -8,6 +8,7 @@ const getProductQuery = /* GraphQL */ ` vendor description descriptionHtml + totalInventory options { id name