UNPKG

1.14 kBJavaScriptView Raw
1import { findMatchingVariant } from './findMatchingProductVariant';
2
3/**
4 * TODO Document
5 */
6export const appendOptionsToPayload = (
7 payload,
8 optionSelections,
9 optionCodes = null
10) => {
11 const { item } = payload;
12 const { variants } = item;
13
14 if (!optionCodes) {
15 optionCodes = new Map();
16 for (const option of item.configurable_options) {
17 // There's a type difference in configurable option queries between
18 // cart and product, casting to number is required. Can remove
19 // cast once MC-29839 is resolved.
20 optionCodes.set(Number(option.attribute_id), option.attribute_code);
21 }
22 }
23
24 const options = Array.from(optionSelections, ([id, value]) => ({
25 option_id: id,
26 option_value: value
27 }));
28
29 const selectedVariant = findMatchingVariant({
30 variants,
31 optionCodes,
32 optionSelections
33 });
34
35 if (!selectedVariant) return payload;
36
37 Object.assign(payload, {
38 options,
39 parentSku: item.sku,
40 item: Object.assign({}, selectedVariant.product)
41 });
42
43 return payload;
44};