commerce/framework/reactioncommerce
tedraykov c6df70c34c Graphql codegen change
When generating graphql types, the generation of optional types is
redundant because the value is wrapped in Maybe type anyway.
Removing the redundancy simplifies the type checking whenever generated
types are used.
2021-05-23 19:15:01 +03:00
..
2021-05-23 19:15:01 +03:00
2021-04-28 15:13:35 -05:00
2021-05-23 19:15:01 +03:00
2021-05-23 19:15:01 +03:00
2021-04-23 20:56:55 +04:00
2021-04-28 15:13:35 -05:00
2021-03-30 20:07:48 +04:00
2021-03-30 20:07:48 +04:00
2021-04-28 21:29:09 +04:00

Table of Contents

Shopify Storefront Data Hooks

Collection of hooks and data fetching functions to integrate Shopify in a React application. Designed to work with Next.js Commerce.

Getting Started

  1. Install dependencies:
yarn install shopify-buy
yarn install -D @types/shopify-buy
  1. Environment variables need to be set:
SHOPIFY_STORE_DOMAIN=
SHOPIFY_STOREFRONT_ACCESS_TOKEN=
NEXT_PUBLIC_SHOPIFY_STORE_DOMAIN=
NEXT_PUBLIC_SHOPIFY_STOREFRONT_ACCESS_TOKEN=
  1. Point the framework to shopify by updating tsconfig.json:
"@framework/*": ["framework/shopify/*"],
"@framework": ["framework/shopify"]

Modifications

These modifications are temporarily until contributions are made to remove them.

Adding item to Cart

// components/product/ProductView/ProductView.tsx
const ProductView: FC<Props> = ({ product }) => {
  const addToCart = async () => {
    setLoading(true)
    try {
      await addItem({
        productId: product.id,
        variantId: variant ? variant.id : product.variants[0].id,
      })
      openSidebar()
      setLoading(false)
    } catch (err) {
      setLoading(false)
    }
  }
}

Proceed to Checkout

// components/cart/CartSidebarView/CartSidebarView.tsx
import { useCommerce } from '@framework'

const CartSidebarView: FC = () => {
  const { checkout } = useCommerce()
  return (
    <Button href={checkout.webUrl} Component="a" width="100%">
      Proceed to Checkout
    </Button>
  )
}

General Usage

CommerceProvider

Provider component that creates the commerce context for children.

import { CommerceProvider } from '@framework'

const App = ({ children }) => {
  return <CommerceProvider locale={locale}>{children}</CommerceProvider>
}

export default App

useCommerce

Returns the configs that are defined in the nearest CommerceProvider. Also provides access to Shopify's checkout and shop.

import { useCommerce } from 'nextjs-commerce-shopify'

const { checkout, shop } = useCommerce()
  • checkout: The information required to checkout items and pay (Documentation).
  • shop: Represents a collection of the general settings and information about the shop (Documentation).

Hooks

usePrice

Display the product variant price according to currency and locale.

import usePrice from '@framework/product/use-price'

const { price } = usePrice({
  amount,
})

Takes in either amount or variant:

  • amount: A price value for a particular item if the amount is known.
  • variant: A shopify product variant. Price will be extracted from the variant.

useAddItem

import { useAddItem } from '@framework/cart'

const AddToCartButton = ({ variantId, quantity }) => {
  const addItem = useAddItem()

  const addToCart = async () => {
    await addItem({
      variantId,
    })
  }

  return <button onClick={addToCart}>Add To Cart</button>
}

useRemoveItem

import { useRemoveItem } from '@framework/cart'

const RemoveButton = ({ item }) => {
  const removeItem = useRemoveItem()

  const handleRemove = async () => {
    await removeItem({ id: item.id })
  }

  return <button onClick={handleRemove}>Remove</button>
}

useUpdateItem

import { useUpdateItem } from '@framework/cart'

const CartItem = ({ item }) => {
  const [quantity, setQuantity] = useState(item.quantity)
  const updateItem = useUpdateItem(item)

  const updateQuantity = async (e) => {
    const val = e.target.value
    await updateItem({ quantity: val })
  }

  return (
    <input
      type="number"
      max={99}
      min={0}
      value={quantity}
      onChange={updateQuantity}
    />
  )
}

APIs

Collections of APIs to fetch data from a Shopify store.

The data is fetched using the Shopify JavaScript Buy SDK. Read the Shopify Storefront API reference for more information.

getProduct

Get a single product by its handle.

import getProduct from '@framework/product/get-product'
import { getConfig } from '@framework/api'

const config = getConfig()

const product = await getProduct({
  variables: { slug },
  config,
})

getAllProducts

import getAllProducts from '@framework/product/get-all-products'
import { getConfig } from '@framework/api'

const config = getConfig()

const { products } = await getAllProducts({
  variables: { first: 12 },
  config,
})

getAllCollections

import getAllCollections from '@framework/product/get-all-collections'
import { getConfig } from '@framework/api'

const config = getConfig()

const collections = await getAllCollections({
  config,
})

getAllPages

import getAllPages from '@framework/common/get-all-pages'
import { getConfig } from '@framework/api'

const config = getConfig()

const pages = await getAllPages({
  variables: { first: 12 },
  config,
})