Skip to content

Commit

Permalink
feat: usecartupdateattributes
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyagabriel committed Mar 3, 2025
1 parent 72f9b75 commit d3de2de
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
12 changes: 12 additions & 0 deletions app/data/graphql/storefront/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,3 +467,15 @@ export const CART_FRAGMENT = `#graphql
${OPTION_FRAGMENT}
${SELLING_PLAN_ALLOCATION_FRAGMENT}
` as const;

export const CART_ATTRIBUTES_QUERY = `#graphql
query ProductPage($id: ID!) {
cart: cart(id: $id) {
id
attributes {
key
value
}
}
}
` as const;
1 change: 1 addition & 0 deletions app/hooks/cart/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export {useCartAddDiscountUrl} from './useCartAddDiscountUrl';
export {useCartUpdateAttributes} from './useCartUpdateAttributes';
38 changes: 38 additions & 0 deletions app/hooks/cart/useCartUpdateAttributes.ts
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};
}
22 changes: 22 additions & 0 deletions app/routes/($locale).api.cart-attributes.tsx
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;
}

0 comments on commit d3de2de

Please sign in to comment.