Skip to main content

decodeEncodedVariant
utility

Decodes an encoded option value string into an array of option value combinations.

string
required

Encoded option value string from the Storefront API, e.g. product.encodedVariantExistence or product.encodedVariantAvailability

Decoded option value combinations

DecodedOptionValues

number[][]
Was this section helpful?

Example code

JavaScript

import {decodeEncodedVariant} from '@shopify/hydrogen-react';

// product.options = [
// {
// name: 'Color',
// optionValues: [
// {name: 'Red'},
// {name: 'Blue'},
// {name: 'Green'},
// ]
// },
// {
// name: 'Size',
// optionValues: [
// {name: 'S'},
// {name: 'M'},
// {name: 'L'},
// ]
// }
// ]

const encodedVariantAvailability = 'v1_0:0-2,1:2,';

const decodedVariantAvailability = decodeEncodedVariant(
encodedVariantAvailability,
);

// decodedVariantAvailability
// {
// [0,0], // Red, S
// [0,1], // Red, M
// [0,2], // Red, L
// [1,2] // Blue, L
// }