Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/optimistic-cart-selected-options.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@shopify/hydrogen": patch
---

Include the selected variant's `selectedOptions` in the add-to-cart event detail built by the product form. Optimistic cart lines now carry `merchandise.selectedOptions` during the mutation window, so cart line components that render option names (e.g. `Size: Large`) no longer flicker from the variant title to the resolved options when the add-to-cart mutation completes.
13 changes: 13 additions & 0 deletions packages/hydrogen/src/core/product/product-form.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,7 @@ describe("createProductFormStore", () => {
{
id: "v-red",
title: "Red",
selectedOptions: [{ name: "Color", value: "Red" }],
product: { title: undefined },
image: undefined,
price: { amount: "10.00", currencyCode: "USD" },
Expand All @@ -931,6 +932,17 @@ describe("createProductFormStore", () => {
});
});

it("includes the variant's selectedOptions so the optimistic line can render option names", async () => {
const cartStore = createMockCartStore();
const store = createStore(makeSingleOptionProduct(RED), cartStore);
const event = new Event("submit") as SubmitEvent;

await store.handleFormSubmit(event);

const [, detail] = (cartStore.handleFormSubmit as ReturnType<typeof vi.fn>).mock.calls[0];
expect(detail.products[0].selectedOptions).toEqual([{ name: "Color", value: "Red" }]);
});

it("passes undefined eventDetail when no variant is selected", async () => {
const cartStore = createMockCartStore();
const store = createStore(makeSingleOptionProduct(null), cartStore);
Expand Down Expand Up @@ -960,6 +972,7 @@ describe("createProductFormStore", () => {
{
id: "v-red",
title: "Red",
selectedOptions: [{ name: "Color", value: "Red" }],
product: { title: "Cozy Shirt" },
image: { url: "https://cdn.example.com/shirt.jpg", altText: "Red shirt" },
price: { amount: "10.00", currencyCode: "USD" },
Expand Down
1 change: 1 addition & 0 deletions packages/hydrogen/src/core/product/product-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ function buildAddToCartDetail<TProduct extends ProductInput>(
{
id: selectedVariant.id,
title: selectedVariant.title,
selectedOptions: selectedVariant.selectedOptions,
product: selectedVariant.product ? { title: selectedVariant.product.title } : undefined,
image: selectedVariant.image,
price: selectedVariant.price,
Expand Down
Loading