Browse Source

feat(core): Facet:value pairs can be used in InitialData collection def

Michael Bromley 5 years ago
parent
commit
2dc7f1552a

+ 1 - 1
docs/content/docs/developer-guide/importing-product-data.md

@@ -102,7 +102,7 @@ export const initialData: InitialData = {
 * `defaultZone`: Sets the default shipping & tax zone for the default Channel. The zone must correspond to a value of `zone` set in the `countries` array. 
 * `taxRates`: For each item, a new [TaxCategory]({{< relref "tax-category" >}}) is created, and then a [TaxRate]({{< relref "tax-rate" >}}) is created for each unique zone defined in the `countries` array. 
 * `shippingMethods`: Allows simple flat-rate [ShippingMethods]({{< relref "shipping-method" >}}) to be defined.
-* `collections`: Allows Collections to be created. Currently only collections based on facet values can be created (`code: 'facet-value-filter'`). The `assetPaths` and `facetValueNames` value must correspond to a values specified in the products csv file.
+* `collections`: Allows Collections to be created. Currently, only collections based on facet values can be created (`code: 'facet-value-filter'`). The `assetPaths` and `facetValueNames` value must correspond to a values specified in the products csv file. The name should match the value specified in the product csv file (or can be a normalized - lower-case & hyphenated - version thereof). If there are FacetValues in multiple Facets with the same name, the facet may be specified with a colon delimiter, e.g. `brand:apple`, `flavour: apple`.
 
 ## Populating The Server
 

+ 15 - 1
packages/core/src/data-import/providers/populator/populator.ts

@@ -101,7 +101,21 @@ export class Populator {
         switch (filter.code) {
             case 'facet-value-filter':
                 const facetValueIds = filter.args.facetValueNames
-                    .map(name => allFacetValues.find(fv => fv.name === name))
+                    .map(name =>
+                        allFacetValues.find(fv => {
+                            let facetName;
+                            let valueName;
+                            if (name.includes(':')) {
+                                [facetName, valueName] = name.split(':');
+                                return (
+                                    (fv.name === valueName || fv.code === valueName) &&
+                                    (fv.facet.name === facetName || fv.facet.code === facetName)
+                                );
+                            } else {
+                                return fv.name === valueName || fv.code === valueName;
+                            }
+                        }),
+                    )
                     .filter(notNullOrUndefined)
                     .map(fv => fv.id);
                 return {