Skip to content

Commit cdc7bfc

Browse files
committed
fix: allow variant attribute filtering
1 parent a2b6f23 commit cdc7bfc

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ declare global {
142142
offerVariantURLTemplate?: string;
143143
attributeOverrides?: AttributeOverrides;
144144
siteOverrides?: Record<string, Record<string, unknown>>;
145-
imageParams?: Record<string, string>;
146-
145+
imageParams?: Record<string, string>;q
146+
variantAttributes?: string[];
147147
liveSearchEnabled?: boolean;
148148
confMap: ConfigMap;
149149
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)