1 |
|
2 |
|
3 |
|
4 | export const findMatchingVariant = ({
|
5 | variants,
|
6 | optionCodes,
|
7 | optionSelections
|
8 | }) => {
|
9 | return variants.find(({ attributes, product }) => {
|
10 | const customAttributes = (attributes || []).reduce(
|
11 | (map, { code, value_index }) => new Map(map).set(code, value_index),
|
12 | new Map()
|
13 | );
|
14 |
|
15 | for (const [id, value] of optionSelections) {
|
16 | const code = optionCodes.get(id);
|
17 | const matchesStandardAttribute = product[code] === value;
|
18 | const matchesCustomAttribute = customAttributes.get(code) === value;
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | if (!matchesStandardAttribute && !matchesCustomAttribute) {
|
24 | return false;
|
25 | }
|
26 | }
|
27 |
|
28 |
|
29 |
|
30 | return true;
|
31 | });
|
32 | };
|