thetrevorharmon / gatsby-theme-shopify-manager

The easiest way to start a Shopify shop on Gatsby.
https://gatsby-theme-shopify-manager.netlify.app/
MIT License
121 stars 11 forks source link

Checkout customAttributes? #74

Closed beamercola closed 4 years ago

beamercola commented 4 years ago

I need to add "Gift Notes" to a cart - I noticed this: https://shopify.github.io/js-buy-sdk/#updating-checkout-attributes

Though I'm having trouble implementing this using the library. Is this possible at this time?

client.checkout.updateAttributes(cart.id, {
  customAttributes: [{ key: 'Gift Note', value: 'Test' }],
})
thetrevorharmon commented 4 years ago

I don't think it is yet–my knowledge of updateAttributes is pretty thin. You could make use of useClientUnsafe to get the client object and useCartUnsafe to get the cart id. Something like this:

function MyComponent() {
  const cart = useCartUnsafe()
  const setCart = useSetCartUnsafe()
  const client = useClientUnsafe()

  const newCart = client.checkout.updateAttributes(cart.id, {
    customAttributes: [{ key: 'Gift Note', value: 'Test' }],
  })

  setCart(newCart)

  return (<div />);
}
beamercola commented 4 years ago

Ah this worked perfectly. I wasn't replacing the cart!