-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproductAttributesConfig.js
More file actions
108 lines (96 loc) · 4.27 KB
/
productAttributesConfig.js
File metadata and controls
108 lines (96 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Product records configuration example file
//
// The list of available attributes is documented here:
// https://www.algolia.com/doc/integration/salesforce-commerce-cloud-b2c/indexing/product-indexing/indexing-attributes/#configurable-attributes
// If you want to add additional attributes, change their name or override the existing definitions:
// - Create a `productAttributesConfig.js` file in this directory
// - Upload the cartridge
// - Add the declared attributes to the "Additional Product Attributes" field of the Business Manager
//
// The `productAttributesConfig.js` file has to export an object with the following structure:
const Logger = require('dw/system/Logger');
const productAttributesConfig = {
// object key is the name that will be used in Algolia records
searchRank: {
// Here is an attribute directly present in the dw.catalog.Product object
attribute: 'searchRank',
localized: false,
},
revenueLast7Days: {
// Nested attributes are supported
attribute: 'activeData.revenueWeek',
localized: false,
},
categoryName: {
attribute: 'primaryCategory.displayName',
// Declare if the attribute is localized
localized: true,
},
colorValue: {
// Getting color value for variant level indexing
attribute: function (product) {
if (!product.isVariant()) {
return null;
}
Logger.info('Processing', JSON.stringify(product));
return product.custom.color;
},
localized: false,
},
// Some base attributes are present by default: https://www.algolia.com/doc/integration/salesforce-commerce-cloud-b2c/indexing/product-indexing/indexing-attributes/#base-attributes-non-configurable
// You can declare an empty object to remove them:
in_stock: {},
sizeValue: {
// Getting size values for the siblings of the current variant without using ProductVariationModel
attribute: function (product) {
Logger.info('Getting size values for product ID: {0}', product.ID);
// Ensure we only handle variant products
if (!product.isVariant()) {
return null;
}
const masterProduct = product.getMasterProduct();
if (!masterProduct) {
return null;
}
const sizeValues = new Set();
const siblingVariants = masterProduct.variants;
siblingVariants.toArray().forEach(function (siblingVariant) {
Logger.info('Processing sibling variant with ID: {0}', siblingVariant.ID);
var siblingColor = siblingVariant.custom && siblingVariant.custom.color;
var siblingSize = siblingVariant.custom && siblingVariant.custom.size;
var currentColor = product.custom && product.custom.color;
if (siblingColor && siblingSize && currentColor) {
if (siblingColor === currentColor) {
Logger.info('Adding size value: {0}', siblingSize);
sizeValues.add(siblingSize);
}
}
});
Logger.info(
'Final size values for product ID {0}: {1}',
product.ID,
Array.from(sizeValues).join(', ')
);
return Array.from(sizeValues);
},
localized: false,
},
// You can overwrite the default definition of existing attributes.
// For example, the Base-product record model has a `variants` attribute:
// https://www.algolia.com/doc/integration/salesforce-commerce-cloud-b2c/indexing/product-indexing/indexing-attributes/#product-level-default-model
variants: {
// For more complex computations, you can use a function. The return value will be indexed into Algolia.
// Here, it returns an array containing the variant IDs
attribute: function (product) {
const variants = [];
const variantsIt = product.variants.iterator();
while (variantsIt.hasNext()) {
var variant = variantsIt.next();
variants.push(variant.ID);
}
return variants;
},
localized: true,
},
};
module.exports = productAttributesConfig;