1 | import { findMatchingVariant } from './findMatchingProductVariant';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | export 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 |
|
18 |
|
19 |
|
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 | };
|