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

Fix getProductOption from crashing when one of the optionValues doesn't return a firstSelectableVariant #2704

Merged
merged 5 commits into from
Jan 14, 2025

Conversation

wizardlyhel
Copy link
Contributor

WHY are these changes introduced?

Fixes #2698

On a very rare occasion, optionValues.firstSelectableVariant may return null.

WHAT is this pull request doing?

Make sure getProductOption handles well if encountered a null firstSelectableVariant.

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

Copy link
Contributor

shopify bot commented Jan 10, 2025

Oxygen deployed a preview of your hl-fix-getProductOption-error branch. Details:

Storefront Status Preview link Deployment details Last update (UTC)
third-party-queries-caching ✅ Successful (Logs) Preview deployment Inspect deployment January 10, 202511:29 PM
Skeleton (skeleton.hydrogen.shop) ✅ Successful (Logs) Preview deployment Inspect deployment January 10, 202511:29 PM
classic-remix ✅ Successful (Logs) Preview deployment Inspect deployment January 10, 202511:29 PM
metaobjects ✅ Successful (Logs) Preview deployment Inspect deployment January 10, 202511:29 PM
custom-cart-method ✅ Successful (Logs) Preview deployment Inspect deployment January 10, 202511:29 PM

Learn more about Hydrogen's GitHub integration.

@wizardlyhel wizardlyhel changed the title Fix getProductOption error Fix getProductOption from crashing when one of the optionValues doesn't return a firstSelectableVariant Jan 10, 2025
@wizardlyhel wizardlyhel changed the title Fix getProductOption from crashing when one of the optionValues doesn't return a firstSelectableVariant Fix getProductOption from crashing when one of the optionValues doesn't return a firstSelectableVariant Jan 10, 2025
Comment on lines +243 to +246
// It is possible for firstSelectableVariant to be null
firstOptionValues = product.options[0].optionValues.filter(
(value) => !!value?.firstSelectableVariant,
)[0];
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe this should happen before the above !firstOptionValues?.name check, otherwise we're potentially ending up with an option value without a name

Copy link
Contributor Author

Choose a reason for hiding this comment

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

firstOptionValues is assigned as the first item in product.options[0].optionValues on line 236

Line 243-246 is just reassigning firstOptionValues to ensure it returns an option value that has a firstSelectableVariant defined

@wizardlyhel wizardlyhel merged commit 650d57b into main Jan 14, 2025
12 checks passed
@wizardlyhel wizardlyhel deleted the hl-fix-getProductOption-error branch January 14, 2025 17:44
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.

getProductOptions throws an error on deleted variants
2 participants