mirror of
https://github.com/vercel/commerce.git
synced 2025-07-27 04:01:23 +00:00
Shopify: create checkout on add to cart (#432)
* Create checkout on add to cart * Checkout changes * Revert files * Fix checkout
This commit is contained in:
@@ -7,27 +7,39 @@ import {
|
||||
} from '../const'
|
||||
|
||||
import checkoutCreateMutation from './mutations/checkout-create'
|
||||
import { CheckoutCreatePayload } from '../schema'
|
||||
import {
|
||||
CheckoutCreatePayload,
|
||||
CheckoutLineItemInput,
|
||||
Mutation,
|
||||
MutationCheckoutCreateArgs,
|
||||
} from '../schema'
|
||||
import { FetcherOptions } from '@commerce/utils/types'
|
||||
|
||||
export const checkoutCreate = async (
|
||||
fetch: any
|
||||
fetch: <T = any, B = Body>(options: FetcherOptions<B>) => Promise<T>,
|
||||
lineItems: CheckoutLineItemInput[]
|
||||
): Promise<CheckoutCreatePayload> => {
|
||||
const data = await fetch({
|
||||
const { checkoutCreate } = await fetch<Mutation, MutationCheckoutCreateArgs>({
|
||||
query: checkoutCreateMutation,
|
||||
variables: {
|
||||
input: { lineItems },
|
||||
},
|
||||
})
|
||||
|
||||
const checkout = data.checkoutCreate?.checkout
|
||||
const checkoutId = checkout?.id
|
||||
const checkout = checkoutCreate?.checkout
|
||||
|
||||
if (checkoutId) {
|
||||
if (checkout) {
|
||||
const checkoutId = checkout?.id
|
||||
const options = {
|
||||
expires: SHOPIFY_COOKIE_EXPIRE,
|
||||
}
|
||||
Cookies.set(SHOPIFY_CHECKOUT_ID_COOKIE, checkoutId, options)
|
||||
Cookies.set(SHOPIFY_CHECKOUT_URL_COOKIE, checkout.webUrl, options)
|
||||
if (checkout?.webUrl) {
|
||||
Cookies.set(SHOPIFY_CHECKOUT_URL_COOKIE, checkout.webUrl, options)
|
||||
}
|
||||
}
|
||||
|
||||
return checkout
|
||||
return checkoutCreate!
|
||||
}
|
||||
|
||||
export default checkoutCreate
|
||||
|
@@ -27,16 +27,15 @@ export type CheckoutPayload =
|
||||
| CheckoutQuery
|
||||
|
||||
const checkoutToCart = (checkoutPayload?: Maybe<CheckoutPayload>): Cart => {
|
||||
const checkout = checkoutPayload?.checkout
|
||||
throwUserErrors(checkoutPayload?.checkoutUserErrors)
|
||||
|
||||
if (!checkout) {
|
||||
if (!checkoutPayload?.checkout) {
|
||||
throw new CommerceError({
|
||||
message: 'Missing checkout object from response',
|
||||
})
|
||||
}
|
||||
|
||||
return normalizeCart(checkout)
|
||||
return normalizeCart(checkoutPayload?.checkout)
|
||||
}
|
||||
|
||||
export default checkoutToCart
|
||||
|
Reference in New Issue
Block a user