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

Store cart ID in cookies #721

Merged
merged 3 commits into from
Apr 4, 2023
Merged

Store cart ID in cookies #721

merged 3 commits into from
Apr 4, 2023

Conversation

scottdixon
Copy link
Contributor

@scottdixon scottdixon commented Mar 27, 2023

Hey @lordofthecactus! Let me know what you think of this approach.

WHY are these changes introduced?

When switching from Shopify's Online Store to Hydrogen, existing customer carts are lost unless the merchant updates their liquid theme to use the Storefront API.

Soon, liquid/Storefront API carts will be interoperable - carts created on a traditional liquid storefront can be managed via the Storefront API and vice versa.

This change leverages the new functionality and shows an example of how to switch between Online Store/Hydrogen without losing carts.

WHAT is this pull request doing?

Updates the Demo Store template to use the same cart cookie set by the traditional Online Store channel.

HOW to test your changes?

  1. Enable cart_repository_online_store_path on your test store.
  2. Create a cart using the traditional Online Store and copy the cart ID from the cart cookie.
  3. Running Demo Store locally, create the same cart cookie.
  4. Switch between Hydrogen/Online Store and the cart should remain in sync.

Feel free to use my test store:

PUBLIC_STORE_DOMAIN="test-shop-987.myshopify.com"
PUBLIC_STOREFRONT_API_TOKEN="b5f455ac1cea1522791da1ad0e053559"

Post-merge steps

Will remove most of these docs: https://shopify.dev/docs/custom-storefronts/oxygen/migrate/share-carts

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

Copy link
Contributor

@lordofthecactus lordofthecactus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @scottdixon 👋 PR works well! I have been 🎩 (tophatting) with your demo store and I could go back and forth the online storefront and hydrogen 🔥

One request before merging!

I also did a deep dive on sessions in remix and made sure:

  • Using request.headers is alright
  • The Set-Cookie works as intended and does not affect other parts of the app. Only if we have another Set-Cookie with the same cookie name would this get overwritten.

Also:

  • fixed the types
  • rebased from latests

@@ -0,0 +1,5 @@
---
'@shopify/hydrogen': minor
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be a patch version. We are using calver, for example 2023.1.0 for jan 2023. So we are using the minor part of the version for the month.

Also, could you describe the changes as @wizardlyhel did here?
https://github.com/Shopify/hydrogen/blob/d827cc2d66dc08c3ce7da15a0723b061dd8039f5/.changeset/angry-months-pay.md

@@ -71,6 +69,7 @@ export async function action({request, context}: ActionArgs) {

break;
case CartAction.REMOVE_FROM_CART:
invariant(cartId, 'Missing cartId');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@scottdixon cart was complaining here because when you parsed the cookie with getCartId the function would have string | undefined union.

Then some of the actions below required to have a string. So instead I just did a check with invariant if the cartId actually exists.

@lordofthecactus
Copy link
Contributor

@scottdixon I'm also wondering if we should update to add some text here https://shopify.dev/docs/custom-storefronts/hydrogen/cart/add#persist-and-return-the-cart about this.
Maybe some sort of note, that if cart wants to be shared with online storefront, to setup a plan cookie, not within session

@scottdixon
Copy link
Contributor Author

Thanks so much @lordofthecactus! I've updated changeset and added a note to the cart docs as part of this PR which will roll out once the feature is live. LMK if I've missed anything 🙏

Copy link
Contributor

@lordofthecactus lordofthecactus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM ✅

@lordofthecactus lordofthecactus merged commit 5420c9f into 2023-01 Apr 4, 2023
@lordofthecactus lordofthecactus deleted the sd-cart-cookie branch April 4, 2023 07:47
@github-actions github-actions bot mentioned this pull request Apr 4, 2023
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.

2 participants