Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add gtin attribute to the google feed app #1186

Merged
merged 3 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/soft-shoes-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"products-feed": minor
---

You can now map GTIN attribute from Saleor to your Google Feed.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const exampleAttributeMappingConfig: RootConfig["attributeMapping"] = {
patternAttributeIds: [],
materialAttributeIds: [],
sizeAttributeIds: [],
gtinAttributeIds: [],
};

const exampleTitleTemplate: RootConfig["titleTemplate"] =
Expand Down Expand Up @@ -52,6 +53,7 @@ describe("AppConfig", function () {
patternAttributeIds: [],
materialAttributeIds: [],
sizeAttributeIds: [],
gtinAttributeIds: [],
},
titleTemplate: "{{variant.product.name}} - {{variant.name}}",
imageSize: 1024,
Expand Down Expand Up @@ -84,6 +86,7 @@ describe("AppConfig", function () {
patternAttributeIds: [],
materialAttributeIds: [],
sizeAttributeIds: [],
gtinAttributeIds: [],
},
titleTemplate: "{{variant.product.name}} - {{variant.name}}",
imageSize: 1024,
Expand Down Expand Up @@ -115,6 +118,7 @@ describe("AppConfig", function () {
patternAttributeIds: [],
materialAttributeIds: [],
sizeAttributeIds: [],
gtinAttributeIds: [],
},
titleTemplate: "{{ variant.name }}",
imageSize: 1024,
Expand All @@ -138,6 +142,7 @@ describe("AppConfig", function () {
patternAttributeIds: [],
materialAttributeIds: [],
sizeAttributeIds: [],
gtinAttributeIds: [],
},
titleTemplate: "{{ variant.name }}",
imageSize: 1024,
Expand Down Expand Up @@ -167,6 +172,7 @@ describe("AppConfig", function () {
patternAttributeIds: [],
materialAttributeIds: [],
sizeAttributeIds: ["size-id"],
gtinAttributeIds: [],
},
titleTemplate: "{{ variant.product.name }} - {{ variant.name }}",
imageSize: 1024,
Expand Down Expand Up @@ -194,6 +200,7 @@ describe("AppConfig", function () {
patternAttributeIds: [],
materialAttributeIds: [],
sizeAttributeIds: ["size-id"],
gtinAttributeIds: [],
},
titleTemplate: "{{ variant.product.name }} - {{ variant.name }}",
imageSize: 1024,
Expand Down Expand Up @@ -225,6 +232,7 @@ describe("AppConfig", function () {
patternAttributeIds: [],
materialAttributeIds: [],
sizeAttributeIds: ["size-id"],
gtinAttributeIds: [],
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const attributeMappingSchema = z.object({
sizeAttributeIds: z.array(z.string()).default([]),
materialAttributeIds: z.array(z.string()).default([]),
patternAttributeIds: z.array(z.string()).default([]),
gtinAttributeIds: z.array(z.string()).default([]),
});

const s3ConfigSchema = z.object({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ export const AttributeMappingConfigurationForm = (props: Props) => {
label="Size attributes"
options={options}
/>
<Multiselect
control={control}
name="gtinAttributeIds"
label="GTIN attributes"
options={options}
/>
<Box display={"flex"} flexDirection={"row"} gap={4} justifyContent={"flex-end"}>
<Button type="submit" variant="primary">
Save mapping
Expand Down Expand Up @@ -98,7 +104,7 @@ export const ConnectedAttributeMappingForm = () => {
async (data: AttributeMappingConfiguration) => {
mutate(data);
},
[mutate]
[mutate],
);

const formData: AttributeMappingConfiguration = useMemo(() => {
Expand All @@ -112,6 +118,7 @@ export const ConnectedAttributeMappingForm = () => {
brandAttributeIds: [],
patternAttributeIds: [],
materialAttributeIds: [],
gtinAttributeIds: [],
};
}, [data]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe("attribute-mapping", () => {
},
values: [],
},
])
]),
).toStrictEqual("Red");
});

Expand Down Expand Up @@ -111,7 +111,7 @@ describe("attribute-mapping", () => {
},
],
},
])
]),
).toStrictEqual("Red/Blue/Yellow");
});
});
Expand All @@ -130,7 +130,7 @@ describe("attribute-mapping", () => {
product: productBase,
attributes: [],
},
})
}),
).toStrictEqual(undefined);
});

Expand All @@ -153,14 +153,16 @@ describe("attribute-mapping", () => {
patternAttributeIds: ["pattern-id"],
materialAttributeIds: ["material-id"],
sizeAttributeIds: ["size-id"],
gtinAttributeIds: ["gtin-id"],
},
})
}),
).toStrictEqual({
material: undefined,
color: undefined,
size: undefined,
brand: undefined,
pattern: undefined,
gtin: undefined,
});
});

Expand Down Expand Up @@ -246,6 +248,16 @@ describe("attribute-mapping", () => {
},
],
},
{
attribute: {
id: "gtin-id",
},
values: [
{
name: "01234500001-0",
},
],
},
],
},
attributeMapping: {
Expand All @@ -254,14 +266,16 @@ describe("attribute-mapping", () => {
materialAttributeIds: ["material-id"],
sizeAttributeIds: ["size-id"],
patternAttributeIds: ["pattern-id"],
gtinAttributeIds: ["gtin-id"],
},
})
}),
).toStrictEqual({
material: "Cotton",
color: "Red/Black",
size: "XL",
brand: "Saleor",
pattern: "Plain",
gtin: "01234500001-0",
});
});
});
Expand Down
18 changes: 12 additions & 6 deletions apps/products-feed/src/modules/google-feed/attribute-mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface GetMappedAttributesArgs {
}

export const attributeArrayToValueString = (
attributes?: GoogleFeedProductVariantFragment["attributes"]
attributes?: GoogleFeedProductVariantFragment["attributes"],
) => {
if (!attributes?.length) {
return;
Expand Down Expand Up @@ -35,35 +35,41 @@ export const getMappedAttributes = ({
const attributes = variant.attributes.concat(variant.product.attributes);

const materialAttributes = attributes.filter((a) =>
mapping.materialAttributeIds.includes(a.attribute.id)
mapping.materialAttributeIds.includes(a.attribute.id),
);
const materialValue = attributeArrayToValueString(materialAttributes);

const brandAttributes = attributes.filter((a) =>
mapping.brandAttributeIds.includes(a.attribute.id)
mapping.brandAttributeIds.includes(a.attribute.id),
);
const brandValue = attributeArrayToValueString(brandAttributes);

const colorAttributes = attributes.filter((a) =>
mapping.colorAttributeIds.includes(a.attribute.id)
mapping.colorAttributeIds.includes(a.attribute.id),
);
const colorValue = attributeArrayToValueString(colorAttributes);

const patternAttributes = attributes.filter((a) =>
mapping.patternAttributeIds.includes(a.attribute.id)
mapping.patternAttributeIds.includes(a.attribute.id),
);
const patternValue = attributeArrayToValueString(patternAttributes);

const sizeAttributes = attributes.filter((a) =>
mapping.sizeAttributeIds.includes(a.attribute.id)
mapping.sizeAttributeIds.includes(a.attribute.id),
);
const sizeValue = attributeArrayToValueString(sizeAttributes);

const gtinAttributes = attributes.filter((a) =>
mapping.gtinAttributeIds.includes(a.attribute.id),
);
const gtinValue = attributeArrayToValueString(gtinAttributes);

return {
material: materialValue,
brand: brandValue,
color: colorValue,
size: sizeValue,
pattern: patternValue,
gtin: gtinValue,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export const generateGoogleXmlFeed = ({
brand: attributes?.brand,
pattern: attributes?.pattern,
size: attributes?.size,
gtin: attributes?.gtin,
weight,
...pricing,
});
Expand Down
10 changes: 10 additions & 0 deletions apps/products-feed/src/modules/google-feed/product-to-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,16 @@ export const productToProxy = (p: ProductEntry) => {
});
}

if (p.gtin) {
item.push({
"g:gtin": [
{
"#text": p.gtin,
},
],
});
}

return {
item,
};
Expand Down
1 change: 1 addition & 0 deletions apps/products-feed/src/modules/google-feed/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export type ProductEntry = {
brand?: string;
pattern?: string;
weight?: string;
gtin?: string;
};

export type ShopDetailsEntry = {
Expand Down
3 changes: 2 additions & 1 deletion cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@
"OTLP",
"FAAS",
"millis",
"Parentfor"
"Parentfor",
"GTIN"
],
"ignorePaths": [
"node_modules",
Expand Down
Loading