Skip to content

Handle empty unitEnum in getUnitString function#938

Open
lynchv wants to merge 1 commit intomainfrom
vl-02-12-handle_empty_unitenum_in_getunitstring_function
Open

Handle empty unitEnum in getUnitString function#938
lynchv wants to merge 1 commit intomainfrom
vl-02-12-handle_empty_unitenum_in_getunitstring_function

Conversation

@lynchv
Copy link

@lynchv lynchv commented Feb 12, 2026

TL;DR

Closes https://github.com/shop/issues-headless/issues/1281

Added handling for null or undefined unit values in the getUnitString function.

Inital Error

TypeError: Cannot read properties of null (reading 'toLowerCase')

Why make this change?

When a product variant has unitPrice but its unitPriceMeasurement.referenceUnit is null, getUnitString() calls .toLowerCase() on null, crashing the render. This cascading failure then causes a secondary error when downstream code accesses .cart on the undefined result of the failed component initialization.

Copy link
Author

lynchv commented Feb 12, 2026

This stack of pull requests is managed by Graphite. Learn more about stacking.

@lynchv lynchv marked this pull request as ready for review February 12, 2026 16:43
Copy link
Contributor

@kdaviduik kdaviduik left a comment

Choose a reason for hiding this comment

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

@@ -1,5 +1,7 @@
function getUnitString(unitEnum) {
if(unitEnum === 'L') {
if (!unitEnum) return '';
Copy link
Contributor

@kdaviduik kdaviduik Feb 16, 2026

Choose a reason for hiding this comment

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

Fixing the issue purely at this level returns an empty string, causing the template to render "$10.00 /" (price with trailing slash, no unit). This looks broken.

Imo we should add in a check for this inside of showUnitPrice

get showUnitPrice() {
    if (!this.selectedVariant ||
        !this.selectedVariant.unitPrice ||
        !this.options.contents.unitPrice) {
      return false;
    }
    // Add this validation
    const measurement = this.selectedVariant.unitPriceMeasurement;
    if (!measurement || !measurement.referenceUnit) {
      return false;
    }
    return true;
  }

Also this change needs tests

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.

3 participants