Adding an example for B2B
Adding an example for B2B, along with an update to the customer account client to support a storefront customer access token via the storefrontCustomerAccessTokenCreate mutation.
WHY are these changes introduced?
Resolves #1677
Hydrogen utilities method for b2b merchant to use.
here is the flow for a typical B2B Hydrogen store
- Land on the site and Login
- Take the
customerAccountAccessToken, and exchange it forstorefrontCustomerAccountAccessTokenimmediately after authorize - At this point if the customer have access to 1 location, set that to update BuyerIdentityUpdate. Or if the customer have multiple locations, FORCE show (dont show anything else) a location selector and set it to cookie + update BuyerIdentityUpdate
- Offer a location selector in the header menu to make changes to the location in the future.
- Ensure Cart is create is created with buyer identity
- At this point use the buyer in session to call SF API query with buyer in context
WHAT is this pull request doing?
Encapsulating common tasks into hydrogen utilities
- In
storefrontclient, ifb2boption is true, a query variable will get automatically created by usingbuyer = customerAccount.UNSTABLE_getBuyer() - In
customerAccount, at the end ofauthorize(), do a token exchange and saved to session. - In
customerAccount, exposeUNSTABLE_setBuyerandUNSTABLE_getBuyerto get and set Buyer on the session. the getter will do a login + expire check, if customerAccessToken is expired and need refresh, the storefrontCustomerAccessToken will automatically follow as well - in
createCartHandler, when we create the cart, the mutation will be run with buyerIdentity coming fromcustomerAccount.UNSTABLE_getBuyer()directly
HOW to test your changes?
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
Oxygen deployed a preview of your df/b2b-example branch. Details:
| Storefront | Status | Preview link | Deployment details | Last update (UTC) |
|---|---|---|---|---|
| subscriptions | ✅ Successful (Logs) | Preview deployment | Inspect deployment | May 7, 2024 7:48 PM |
| custom-cart-method | ✅ Successful (Logs) | Preview deployment | Inspect deployment | May 7, 2024 7:48 PM |
| third-party-queries-caching | ✅ Successful (Logs) | Preview deployment | Inspect deployment | May 7, 2024 7:48 PM |
| vite | ✅ Successful (Logs) | Preview deployment | Inspect deployment | May 7, 2024 7:48 PM |
| optimistic-cart-ui | ✅ Successful (Logs) | Preview deployment | Inspect deployment | May 7, 2024 7:48 PM |
| Skeleton (skeleton.hydrogen.shop) | ✅ Successful (Logs) | Preview deployment | Inspect deployment | May 7, 2024 7:48 PM |
Learn more about Hydrogen's GitHub integration.
We detected some changes in packages/*/package.json or packages/*/src, and there are no updates in the .changeset.
If the changes are user-facing and should cause a version bump, run npm run changeset add to track your changes and include them in the next release CHANGELOG.
If you are making simple updates to examples or documentation, you do not need to add a changeset.
Got error in logged out state on the product page. Looks like just need to avoid destructuring when buyer doesn't exists.
I also did some clean up on the createCartHandler updates. I push the customer account changes into the individual mutation queries themselves to handle adding the customerAccountToken to buyerIdentity