Skip to content

Commit c926b3f

Browse files
authored
fix: allow variant attribute filtering (#70)
* fix: allow variant attribute filtering * fix: add to RawConfigEntry schema * fix: add to config schema
1 parent a2b6f23 commit c926b3f

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

src/schemas/Config.js

+4
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ const ConfigEntry = {
6161
type: 'array',
6262
items: { type: 'string' },
6363
},
64+
variantAttributes: {
65+
type: 'array',
66+
items: { type: 'string' },
67+
},
6468
},
6569
};
6670

src/templates/html/HTMLTemplate.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,8 @@ ${this.variants?.map((v) => /* html */`\
372372
<div></div>
373373
<div></div>
374374
</div>
375-
${v.attributes?.map((attribute) => /* html */`\
375+
${v.attributes?.filter((a) => (this.ctx.config?.variantAttributes
376+
? this.ctx.config.variantAttributes.includes(a.name) : true)).map((attribute) => /* html */`\
376377
<div>
377378
<div>attribute</div>
378379
<div>${attribute.name}</div>

src/types.d.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ declare global {
8484

8585
// required for non-base entries
8686
pageType: 'product' | string;
87+
88+
/**
89+
* Attributes to include in the variant attributes table
90+
*/
91+
variantAttributes?: string[];
8792
}
8893

8994
/**
@@ -143,7 +148,7 @@ declare global {
143148
attributeOverrides?: AttributeOverrides;
144149
siteOverrides?: Record<string, Record<string, unknown>>;
145150
imageParams?: Record<string, string>;
146-
151+
variantAttributes?: string[];
147152
liveSearchEnabled?: boolean;
148153
confMap: ConfigMap;
149154
confMapStr: string;

test/templates/html/index.test.js

+19
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,25 @@ describe('Render Product HTML', () => {
255255
assert.strictEqual(variantAttributesTable, null, 'Variant attributes table should not be rendered');
256256
});
257257

258+
it('should filter variant attributes if variantAttributes is set', () => {
259+
config.variantAttributes = ['criteria_1', 'criteria_2'];
260+
const html = htmlTemplateFromContext(DEFAULT_CONTEXT({ config }), product, variations).render();
261+
dom = new JSDOM(html);
262+
document = dom.window.document;
263+
const variantAttributesTable = document.querySelector('.variant-attributes');
264+
const variantAttributes = variantAttributesTable.querySelectorAll('.variant-attributes > div');
265+
assert.strictEqual(variantAttributes.length, 9, 'Variant attributes table should be rendered with 2 attributes');
266+
assert.strictEqual(variantAttributes[0].querySelector('div:nth-child(1)').textContent, 'sku', 'Sku should be rendered');
267+
assert.strictEqual(variantAttributes[1].querySelector('div:nth-child(2)').textContent, 'criteria_1', 'Variant attribute 1 should be rendered');
268+
assert.strictEqual(variantAttributes[2].querySelector('div:nth-child(2)').textContent, 'criteria_2', 'Variant attribute 2 should be rendered');
269+
assert.strictEqual(variantAttributes[3].querySelector('div:nth-child(1)').textContent, 'sku', 'Sku should not be rendered');
270+
assert.strictEqual(variantAttributes[4].querySelector('div:nth-child(2)').textContent, 'criteria_1', 'Variant attribute 4 should not be rendered');
271+
assert.strictEqual(variantAttributes[5].querySelector('div:nth-child(2)').textContent, 'criteria_2', 'Variant attribute 5 should not be rendered');
272+
assert.strictEqual(variantAttributes[6].querySelector('div:nth-child(1)').textContent, 'sku', 'Sku should not be rendered');
273+
assert.strictEqual(variantAttributes[7].querySelector('div:nth-child(2)').textContent, 'criteria_1', 'Variant attribute 4 should not be rendered');
274+
assert.strictEqual(variantAttributes[8].querySelector('div:nth-child(2)').textContent, 'criteria_2', 'Variant attribute 5 should not be rendered');
275+
});
276+
258277
it('template should allow for missing prices', () => {
259278
config.confMap = {
260279
'/us/p/{{urlkey}}/{{sku}}': {},

0 commit comments

Comments
 (0)