diff --git a/packages/medusa-plugin-contentful/src/services/contentful.js b/packages/medusa-plugin-contentful/src/services/contentful.js index edd046c8a5..4076d5e1d1 100644 --- a/packages/medusa-plugin-contentful/src/services/contentful.js +++ b/packages/medusa-plugin-contentful/src/services/contentful.js @@ -174,7 +174,7 @@ class ContentfulService extends BaseService { "en-US": variantLinks, }, [this.getCustomField("options", "product")]: { - "en-US": p.options, + "en-US": this.transformMedusaIds(p.options), }, [this.getCustomField("medusaId", "product")]: { "en-US": p.id, @@ -285,10 +285,10 @@ class ContentfulService extends BaseService { "en-US": v.sku, }, [this.getCustomField("prices", "variant")]: { - "en-US": v.prices, + "en-US": this.transformMedusaIds(v.prices), }, [this.getCustomField("options", "variant")]: { - "en-US": v.options, + "en-US": this.transformMedusaIds(v.options), }, [this.getCustomField("medusaId", "variant")]: { "en-US": v.id, @@ -476,7 +476,7 @@ class ContentfulService extends BaseService { "en-US": p.description, }, [this.getCustomField("options", "product")]: { - "en-US": p.options, + "en-US": this.transformMedusaIds(p.options), }, [this.getCustomField("variants", "product")]: { "en-US": variantLinks, @@ -538,9 +538,8 @@ class ContentfulService extends BaseService { "en-US": p.collection.title, } - productEntryFields[ - this.getCustomField("collection", "product") - ] = collection + productEntryFields[this.getCustomField("collection", "product")] = + collection } if (p.tags) { @@ -624,10 +623,10 @@ class ContentfulService extends BaseService { "en-US": v.sku, }, [this.getCustomField("options", "variant")]: { - "en-US": v.options, + "en-US": this.transformMedusaIds(v.options), }, [this.getCustomField("prices", "variant")]: { - "en-US": v.prices, + "en-US": this.transformMedusaIds(v.prices), }, [this.getCustomField("medusaId", "variant")]: { "en-US": v.id, @@ -734,6 +733,28 @@ class ContentfulService extends BaseService { } } + transformMedusaIds(objOrArray) { + let input = objOrArray + let isArray = true + if (!Array.isArray(objOrArray)) { + input = [objOrArray] + isArray = false + } + + let output = [] + for (const obj of input) { + let transformed = Object.assign({}, objOrArray) + transformed.medusaId = output.id + output.push(transformed) + } + + if (!isArray) { + return output[0] + } + + return output + } + async getType(type) { const environment = await this.getContentfulEnvironment_() return environment.getContentType(type)