mirror of
https://github.com/vercel/commerce.git
synced 2025-06-19 05:31:22 +00:00
Fix sold out item
This commit is contained in:
parent
d838f34c73
commit
cb79d77c88
@ -146,8 +146,12 @@ const ProductView: FC<Props> = ({ product }) => {
|
|||||||
className={s.button}
|
className={s.button}
|
||||||
onClick={addToCart}
|
onClick={addToCart}
|
||||||
loading={loading}
|
loading={loading}
|
||||||
|
disabled={variant?.availableForSale === false}
|
||||||
>
|
>
|
||||||
Add to Cart
|
{variant?.isInStock === false &&
|
||||||
|
variant?.availableForSale === false
|
||||||
|
? 'Sold Out'
|
||||||
|
: 'Add To Cart'}
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -190,6 +190,10 @@ interface ProductImage {
|
|||||||
interface ProductVariant2 {
|
interface ProductVariant2 {
|
||||||
id: string | number
|
id: string | number
|
||||||
options: ProductOption[]
|
options: ProductOption[]
|
||||||
|
// Indicates whether this product variant is in stock.
|
||||||
|
isInStock?: boolean
|
||||||
|
// Indicates if the product variant is available for sale.
|
||||||
|
availableForSale?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ProductPrice {
|
interface ProductPrice {
|
||||||
|
@ -10,11 +10,14 @@ export const handler: SWRHook<Customer | null> = {
|
|||||||
query: getCustomerQuery,
|
query: getCustomerQuery,
|
||||||
},
|
},
|
||||||
async fetcher({ options, fetch }) {
|
async fetcher({ options, fetch }) {
|
||||||
const data = await fetch<any | null>({
|
const customerAccessToken = getCustomerToken()
|
||||||
...options,
|
if (customerAccessToken) {
|
||||||
variables: { customerAccessToken: getCustomerToken() },
|
const data = await fetch<any | null>({
|
||||||
})
|
...options,
|
||||||
return data.customer ?? null
|
variables: { customerAccessToken },
|
||||||
|
})
|
||||||
|
return data.customer
|
||||||
|
}
|
||||||
},
|
},
|
||||||
useHook: ({ useData }) => (input) => {
|
useHook: ({ useData }) => (input) => {
|
||||||
return useData({
|
return useData({
|
||||||
|
@ -27,22 +27,15 @@ export type CheckoutPayload =
|
|||||||
| CheckoutQuery
|
| CheckoutQuery
|
||||||
|
|
||||||
const checkoutToCart = (checkoutPayload?: Maybe<CheckoutPayload>): Cart => {
|
const checkoutToCart = (checkoutPayload?: Maybe<CheckoutPayload>): Cart => {
|
||||||
if (!checkoutPayload) {
|
|
||||||
throw new CommerceError({
|
|
||||||
message: 'Missing checkout payload from response',
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const checkout = checkoutPayload?.checkout
|
|
||||||
throwUserErrors(checkoutPayload?.checkoutUserErrors)
|
throwUserErrors(checkoutPayload?.checkoutUserErrors)
|
||||||
|
|
||||||
if (!checkout) {
|
if (!checkoutPayload?.checkout) {
|
||||||
throw new CommerceError({
|
throw new CommerceError({
|
||||||
message: 'Missing checkout object from response',
|
message: 'Missing checkout object from response',
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return normalizeCart(checkout)
|
return normalizeCart(checkoutPayload?.checkout)
|
||||||
}
|
}
|
||||||
|
|
||||||
export default checkoutToCart
|
export default checkoutToCart
|
||||||
|
@ -10,11 +10,11 @@ export const getSearchVariables = ({
|
|||||||
let query = ''
|
let query = ''
|
||||||
|
|
||||||
if (search) {
|
if (search) {
|
||||||
query += `product_type:${search} OR title:${search} OR tag:${search}`
|
query += `product_type:${search} OR title:${search} OR tag:${search} `
|
||||||
}
|
}
|
||||||
|
|
||||||
if (brandId) {
|
if (brandId) {
|
||||||
query += `${search ? ' AND ' : ''}vendor:${brandId}`
|
query += `${search ? 'AND ' : ''}vendor:${brandId}`
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@ -53,7 +53,17 @@ const normalizeProductImages = ({ edges }: ImageConnection) =>
|
|||||||
const normalizeProductVariants = ({ edges }: ProductVariantConnection) => {
|
const normalizeProductVariants = ({ edges }: ProductVariantConnection) => {
|
||||||
return edges?.map(
|
return edges?.map(
|
||||||
({
|
({
|
||||||
node: { id, selectedOptions, sku, title, priceV2, compareAtPriceV2 },
|
node: {
|
||||||
|
id,
|
||||||
|
selectedOptions,
|
||||||
|
sku,
|
||||||
|
title,
|
||||||
|
priceV2,
|
||||||
|
compareAtPriceV2,
|
||||||
|
requiresShipping,
|
||||||
|
availableForSale,
|
||||||
|
quantityAvailable,
|
||||||
|
},
|
||||||
}) => {
|
}) => {
|
||||||
return {
|
return {
|
||||||
id,
|
id,
|
||||||
@ -61,7 +71,9 @@ const normalizeProductVariants = ({ edges }: ProductVariantConnection) => {
|
|||||||
sku: sku ?? id,
|
sku: sku ?? id,
|
||||||
price: +priceV2.amount,
|
price: +priceV2.amount,
|
||||||
listPrice: +compareAtPriceV2?.amount,
|
listPrice: +compareAtPriceV2?.amount,
|
||||||
requiresShipping: true,
|
requiresShipping,
|
||||||
|
availableForSale,
|
||||||
|
isInStock: Number(quantityAvailable) > 0,
|
||||||
options: selectedOptions.map(({ name, value }: SelectedOption) => {
|
options: selectedOptions.map(({ name, value }: SelectedOption) => {
|
||||||
const options = normalizeProductOption({
|
const options = normalizeProductOption({
|
||||||
id,
|
id,
|
||||||
|
@ -7,6 +7,7 @@ edges {
|
|||||||
node {
|
node {
|
||||||
id
|
id
|
||||||
title
|
title
|
||||||
|
availableForSale
|
||||||
vendor
|
vendor
|
||||||
handle
|
handle
|
||||||
description
|
description
|
||||||
|
@ -33,6 +33,9 @@ const getProductQuery = /* GraphQL */ `
|
|||||||
id
|
id
|
||||||
title
|
title
|
||||||
sku
|
sku
|
||||||
|
availableForSale
|
||||||
|
quantityAvailable
|
||||||
|
requiresShipping
|
||||||
selectedOptions {
|
selectedOptions {
|
||||||
name
|
name
|
||||||
value
|
value
|
||||||
|
Loading…
x
Reference in New Issue
Block a user