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:
cond0r
2021-09-24 22:55:46 +03:00
committed by GitHub
parent 8e7b942240
commit f9644fecef
6 changed files with 97 additions and 73 deletions

View File

@@ -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

View File

@@ -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