From 2dc7f1552ae67821a2cf354691c94cc73ff9f07a Mon Sep 17 00:00:00 2001 From: Michael Bromley Date: Mon, 15 Feb 2021 13:38:25 +0100 Subject: [PATCH] feat(core): Facet:value pairs can be used in InitialData collection def --- .../developer-guide/importing-product-data.md | 2 +- .../data-import/providers/populator/populator.ts | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/docs/content/docs/developer-guide/importing-product-data.md b/docs/content/docs/developer-guide/importing-product-data.md index c6bf291a41..a7a85a2815 100644 --- a/docs/content/docs/developer-guide/importing-product-data.md +++ b/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 diff --git a/packages/core/src/data-import/providers/populator/populator.ts b/packages/core/src/data-import/providers/populator/populator.ts index ae94f14fd0..87c294cb90 100644 --- a/packages/core/src/data-import/providers/populator/populator.ts +++ b/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 {