mirror of
https://github.com/vercel/commerce.git
synced 2025-06-19 05:31:22 +00:00
Merge branch 'shopify' of https://github.com/vercel/commerce into shopify
This commit is contained in:
commit
bb14651785
@ -1,3 +1,7 @@
|
|||||||
{
|
{
|
||||||
"provider": "shopify"
|
"provider": "bigcommerce",
|
||||||
|
"features": {
|
||||||
|
"wishlist": true,
|
||||||
|
"customCheckout": false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,23 +1,20 @@
|
|||||||
import cn from 'classnames'
|
import cn from 'classnames'
|
||||||
import Image from 'next/image'
|
import Image from 'next/image'
|
||||||
import { NextSeo } from 'next-seo'
|
import { NextSeo } from 'next-seo'
|
||||||
import { FC, useState } from 'react'
|
import { FC, useEffect, useState } from 'react'
|
||||||
import s from './ProductView.module.css'
|
import s from './ProductView.module.css'
|
||||||
|
|
||||||
import { Swatch, ProductSlider } from '@components/product'
|
import { Swatch, ProductSlider } from '@components/product'
|
||||||
import { Button, Container, Text, useUI } from '@components/ui'
|
import { Button, Container, Text, useUI } from '@components/ui'
|
||||||
|
|
||||||
import type { Product } from '@commerce/types'
|
import type { Product } from '@commerce/types'
|
||||||
import usePrice from '@framework/product/use-price'
|
import usePrice from '@framework/product/use-price'
|
||||||
import { useAddItem } from '@framework/cart'
|
import { useAddItem } from '@framework/cart'
|
||||||
|
|
||||||
import { getVariant, SelectedOptions } from '../helpers'
|
import { getVariant, SelectedOptions } from '../helpers'
|
||||||
import WishlistButton from '@components/wishlist/WishlistButton'
|
import WishlistButton from '@components/wishlist/WishlistButton'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
className?: string
|
|
||||||
children?: any
|
children?: any
|
||||||
product: Product
|
product: Product
|
||||||
|
className?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
const ProductView: FC<Props> = ({ product }) => {
|
const ProductView: FC<Props> = ({ product }) => {
|
||||||
@ -29,12 +26,18 @@ const ProductView: FC<Props> = ({ product }) => {
|
|||||||
})
|
})
|
||||||
const { openSidebar } = useUI()
|
const { openSidebar } = useUI()
|
||||||
const [loading, setLoading] = useState(false)
|
const [loading, setLoading] = useState(false)
|
||||||
const [choices, setChoices] = useState<SelectedOptions>({
|
const [choices, setChoices] = useState<SelectedOptions>({})
|
||||||
size: null,
|
|
||||||
color: null,
|
useEffect(() => {
|
||||||
})
|
// Selects the default option
|
||||||
|
product.variants[0].options?.forEach((v) => {
|
||||||
|
setChoices((choices) => ({
|
||||||
|
...choices,
|
||||||
|
[v.displayName.toLowerCase()]: v.values[0].label.toLowerCase(),
|
||||||
|
}))
|
||||||
|
})
|
||||||
|
}, [])
|
||||||
|
|
||||||
// Select the correct variant based on choices
|
|
||||||
const variant = getVariant(product, choices)
|
const variant = getVariant(product, choices)
|
||||||
|
|
||||||
const addToCart = async () => {
|
const addToCart = async () => {
|
||||||
@ -143,7 +146,6 @@ const ProductView: FC<Props> = ({ product }) => {
|
|||||||
className={s.button}
|
className={s.button}
|
||||||
onClick={addToCart}
|
onClick={addToCart}
|
||||||
loading={loading}
|
loading={loading}
|
||||||
disabled={!variant && product.options.length > 0}
|
|
||||||
>
|
>
|
||||||
Add to Cart
|
Add to Cart
|
||||||
</Button>
|
</Button>
|
||||||
|
@ -1,9 +1,5 @@
|
|||||||
import type { Product } from '@commerce/types'
|
import type { Product } from '@commerce/types'
|
||||||
|
export type SelectedOptions = Record<string, string | null>
|
||||||
export type SelectedOptions = {
|
|
||||||
size: string | null
|
|
||||||
color: string | null
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getVariant(product: Product, opts: SelectedOptions) {
|
export function getVariant(product: Product, opts: SelectedOptions) {
|
||||||
const variant = product.variants.find((variant) => {
|
const variant = product.variants.find((variant) => {
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
"site_name": "Next.js Commerce",
|
"site_name": "Next.js Commerce",
|
||||||
"images": [
|
"images": [
|
||||||
{
|
{
|
||||||
"url": "https://shopify.demo.vercel.store/card.png",
|
"url": "/card.png",
|
||||||
"width": 800,
|
"width": 800,
|
||||||
"height": 600,
|
"height": 600,
|
||||||
"alt": "Next.js Commerce"
|
"alt": "Next.js Commerce"
|
||||||
|
@ -99,12 +99,11 @@ export function normalizeProduct(productNode: ShopifyProduct): Product {
|
|||||||
price: money(priceRange?.minVariantPrice),
|
price: money(priceRange?.minVariantPrice),
|
||||||
images: normalizeProductImages(images),
|
images: normalizeProductImages(images),
|
||||||
variants: variants ? normalizeProductVariants(variants) : [],
|
variants: variants ? normalizeProductVariants(variants) : [],
|
||||||
options:
|
options: options
|
||||||
options
|
? options
|
||||||
?.filter(({ name, values }) => {
|
.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
|
||||||
return name !== 'Title' && values !== ['Default Title']
|
.map((o) => normalizeProductOption(o))
|
||||||
})
|
: [],
|
||||||
.map((o) => normalizeProductOption(o)) ?? [],
|
|
||||||
...rest,
|
...rest,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,12 +151,13 @@ function normalizeLineItem({
|
|||||||
path: '',
|
path: '',
|
||||||
discounts: [],
|
discounts: [],
|
||||||
options:
|
options:
|
||||||
variant?.title !== 'Default Title'
|
// By default Shopify adds a default variant with default names, we're removing it. https://community.shopify.com/c/Shopify-APIs-SDKs/Adding-new-product-variant-is-automatically-adding-quot-Default/td-p/358095
|
||||||
? [
|
variant?.title == 'Default Title'
|
||||||
|
? []
|
||||||
|
: [
|
||||||
{
|
{
|
||||||
value: variant?.title,
|
value: variant?.title,
|
||||||
},
|
},
|
||||||
]
|
],
|
||||||
: [],
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,8 +22,8 @@
|
|||||||
"@components/*": ["components/*"],
|
"@components/*": ["components/*"],
|
||||||
"@commerce": ["framework/commerce"],
|
"@commerce": ["framework/commerce"],
|
||||||
"@commerce/*": ["framework/commerce/*"],
|
"@commerce/*": ["framework/commerce/*"],
|
||||||
"@framework": ["framework/shopify"],
|
"@framework": ["framework/bigcommerce"],
|
||||||
"@framework/*": ["framework/shopify/*"]
|
"@framework/*": ["framework/bigcommerce/*"]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"include": ["next-env.d.ts", "**/*.d.ts", "**/*.ts", "**/*.tsx", "**/*.js"],
|
"include": ["next-env.d.ts", "**/*.d.ts", "**/*.ts", "**/*.tsx", "**/*.js"],
|
||||||
|
Loading…
x
Reference in New Issue
Block a user