-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
72f9b75
commit d3de2de
Showing
4 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export {useCartAddDiscountUrl} from './useCartAddDiscountUrl'; | ||
export {useCartUpdateAttributes} from './useCartUpdateAttributes'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import {useCallback} from 'react'; | ||
import {useCart} from '@shopify/hydrogen-react'; | ||
import type { | ||
AttributeInput, | ||
Cart, | ||
} from '@shopify/hydrogen/storefront-api-types'; | ||
|
||
/* Hook that updates shopify cart attributes using the Storefront API query | ||
This hook ensures adding existing attributes added by 3rd party scripts | ||
Note: A common mistake of updating cart attributes with Shopify's | ||
cartAttributesUpdate mutation is that existing attributes need to be included | ||
when adding new attributes. Sometimes 3rd party scripts don't include this. | ||
*/ | ||
|
||
export function useCartUpdateAttributes() { | ||
const {cartAttributesUpdate, id} = useCart(); | ||
|
||
const updateCartAttributes = useCallback( | ||
async (attributes: {key: string; value: string}[]) => { | ||
const response = await fetch(`/api/cart-attributes?id=${id}`, { | ||
method: 'GET', | ||
}); | ||
|
||
const {cart}: {cart: Cart} = await response.json(); | ||
|
||
const existingAttributes = cart?.attributes || []; | ||
|
||
cartAttributesUpdate([ | ||
...existingAttributes, | ||
...attributes, | ||
] as AttributeInput[]); | ||
}, | ||
[id], | ||
); | ||
|
||
return {updateCartAttributes}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import type {LoaderFunctionArgs} from '@shopify/remix-oxygen'; | ||
|
||
import {CART_ATTRIBUTES_QUERY} from '~/data/graphql/storefront/cart'; | ||
|
||
export async function loader({request, context}: LoaderFunctionArgs) { | ||
const {storefront} = context; | ||
const url = new URL(request.url); | ||
|
||
const searchParams = new URLSearchParams(url.search); | ||
const id = String(searchParams.get('id') || ''); | ||
|
||
const cart = await storefront.query(CART_ATTRIBUTES_QUERY, { | ||
variables: { | ||
id, | ||
country: storefront.i18n.country, | ||
language: storefront.i18n.language, | ||
}, | ||
cache: storefront.CacheShort(), | ||
}); | ||
|
||
return cart; | ||
} |