Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow calling the cart .get() method right after cart mutation methods #1865

Merged
merged 1 commit into from Mar 18, 2024

Conversation

blittle
Copy link
Contributor

@blittle blittle commented Mar 18, 2024

WHY are these changes introduced?

Allow calling .get() immediately after cart mutation methods:

import {
  createCartHandler,
  cartGetIdDefault,
  cartSetIdDefault,
} from '@shopify/hydrogen';

const cartHandler = createCartHandler({
  storefront,
  getCartId: cartGetIdDefault(request.headers),
  setCartId: cartSetIdDefault(),
  cartQueryFragment: CART_QUERY_FRAGMENT,
  cartMutateFragment: CART_MUTATE_FRAGMENT,
});

await cartHandler.addLines([{merchandiseId: '...'}]);
// This change fixes a bug where `cart` would be null, even though a
// new cart was created when adding a line item
const cart = await cartHandler.get();

WHAT is this pull request doing?

When calling cart.get(), we only make a request if a cartId exists. The problem is, someone might call a mutation method that creates the cart, and then want to get the cart. If we know the cart has been created, we can internally store the cartId in memory, and only call getCartId() if that internal value is undefined.

HOW to test your changes?

Add await cart.get() right after await cart.addLines(inputs.lines) in cart.ts in the skeleton template..

Make sure the returned value is defined after adding an item to the cart. Also make sure that no cart exists before adding to the cart (clear cookies).

Post-merge steps

Checklist

  • I've read the Contributing Guidelines
  • I've considered possible cross-platform impacts (Mac, Linux, Windows)
  • I've added a changeset if this PR contains user-facing or noteworthy changes
  • I've added tests to cover my changes
  • I've added or updated the documentation

@blittle blittle merged commit 3c8a731 into main Mar 18, 2024
13 checks passed
@blittle blittle deleted the bl-fix-cart branch March 18, 2024 15:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants