diff --git a/packages/medusa-plugin-contentful/src/services/contentful.js b/packages/medusa-plugin-contentful/src/services/contentful.js index 62ce9afc47..9aca32cae9 100644 --- a/packages/medusa-plugin-contentful/src/services/contentful.js +++ b/packages/medusa-plugin-contentful/src/services/contentful.js @@ -315,6 +315,11 @@ class ContentfulService extends BaseService { return this.createProductVariantInContentful(variant) } + const cleanPrices = variants.prices.map((price) => ({ + ...price, + sale_amount: price.sale_amount || undefined, + })) + const variantEntryFields = { ...variantEntry.fields, title: { @@ -327,7 +332,7 @@ class ContentfulService extends BaseService { "en-US": variant.options, }, prices: { - "en-US": variant.prices, + "en-US": cleanPrices, }, objectId: { "en-US": variant._id, diff --git a/packages/medusa/src/api/routes/admin/products/create-product.js b/packages/medusa/src/api/routes/admin/products/create-product.js index dcb92c2720..2164713b39 100644 --- a/packages/medusa/src/api/routes/admin/products/create-product.js +++ b/packages/medusa/src/api/routes/admin/products/create-product.js @@ -20,6 +20,7 @@ export default async (req, res) => { .items({ currency_code: Validator.string().required(), amount: Validator.number().required(), + sale_amount: Validator.number().optional(), }) .required(), options: Validator.array() diff --git a/packages/medusa/src/api/routes/admin/products/create-variant.js b/packages/medusa/src/api/routes/admin/products/create-variant.js index d59034014b..80ade6f570 100644 --- a/packages/medusa/src/api/routes/admin/products/create-variant.js +++ b/packages/medusa/src/api/routes/admin/products/create-variant.js @@ -10,6 +10,7 @@ export default async (req, res) => { .items({ currency_code: Validator.string().required(), amount: Validator.number().required(), + sale_amount: Validator.number().optional(), }) .required(), options: Validator.array() diff --git a/packages/medusa/src/api/routes/admin/products/update-product.js b/packages/medusa/src/api/routes/admin/products/update-product.js index f3c79f2af3..74e512de68 100644 --- a/packages/medusa/src/api/routes/admin/products/update-product.js +++ b/packages/medusa/src/api/routes/admin/products/update-product.js @@ -31,6 +31,7 @@ export default async (req, res) => { region_id: Validator.string(), currency_code: Validator.string(), amount: Validator.number().required(), + sale_amount: Validator.number().optional(), }) .xor("region_id", "currency_code") ), diff --git a/packages/medusa/src/api/routes/admin/products/update-variant.js b/packages/medusa/src/api/routes/admin/products/update-variant.js index 521fc54763..1b34c17faf 100644 --- a/packages/medusa/src/api/routes/admin/products/update-variant.js +++ b/packages/medusa/src/api/routes/admin/products/update-variant.js @@ -13,6 +13,7 @@ export default async (req, res) => { region_id: Validator.string(), currency_code: Validator.string(), amount: Validator.number().required(), + sale_amount: Validator.number().optional(), }) .xor("region_id", "currency_code") ), @@ -45,13 +46,15 @@ export default async (req, res) => { await productVariantService.setRegionPrice( variant_id, price.region_id, - price.amount + price.amount, + price.sale_amount || undefined ) } else { await productVariantService.setCurrencyPrice( variant_id, price.currency_code, - price.amount + price.amount, + price.sale_amount || undefined ) } } diff --git a/packages/medusa/src/models/schemas/money-amount.js b/packages/medusa/src/models/schemas/money-amount.js index 5912fa60b1..3dc0b94930 100644 --- a/packages/medusa/src/models/schemas/money-amount.js +++ b/packages/medusa/src/models/schemas/money-amount.js @@ -8,4 +8,5 @@ export default new mongoose.Schema({ region_id: { type: String }, currency_code: { type: String, required: true }, amount: { type: Number, required: true, min: 0 }, + sale_amount: { type: Number, min: 0 }, }) diff --git a/packages/medusa/src/services/product-variant.js b/packages/medusa/src/services/product-variant.js index 02778fe6c3..0c781fa85b 100644 --- a/packages/medusa/src/services/product-variant.js +++ b/packages/medusa/src/services/product-variant.js @@ -150,9 +150,10 @@ class ProductVariantService extends BaseService { * @param {string} variantId - the id of the variant to set prices for * @param {string} currencyCode - the currency to set prices for * @param {number} amount - the amount to set the price to + * @param {number} saleAmount - the sale amount to set the price to * @return {Promise} the result of the update operation */ - async setCurrencyPrice(variantId, currencyCode, amount) { + async setCurrencyPrice(variantId, currencyCode, amount, saleAmount) { const variant = await this.retrieve(variantId) // If prices already exist we need to update all prices with the same @@ -162,6 +163,7 @@ class ProductVariantService extends BaseService { const newPrices = variant.prices.map(moneyAmount => { if (moneyAmount.currency_code === currencyCode) { moneyAmount.amount = amount + moneyAmount.sale_amount = saleAmount if (!moneyAmount.region_id) { foundDefault = true @@ -176,6 +178,7 @@ class ProductVariantService extends BaseService { if (!foundDefault) { newPrices.push({ currency_code: currencyCode, + sale_amount: saleAmount, amount, }) } @@ -210,6 +213,7 @@ class ProductVariantService extends BaseService { prices: [ { currency_code: currencyCode, + sale_amount: saleAmount, amount, }, ], @@ -238,18 +242,28 @@ class ProductVariantService extends BaseService { const region = await this.regionService_.retrieve(regionId) let price - variant.prices.forEach(({ region_id, amount, currency_code }) => { - if (!price && !region_id && currency_code === region.currency_code) { - // If we haven't yet found a price and the current money amount is - // the default money amount for the currency of the region we have found - // a possible price match - price = amount - } else if (region_id === region._id) { - // If the region matches directly with the money amount this is the best - // price - price = amount + variant.prices.forEach( + ({ region_id, amount, sale_amount, currency_code }) => { + if (!price && !region_id && currency_code === region.currency_code) { + // If we haven't yet found a price and the current money amount is + // the default money amount for the currency of the region we have found + // a possible price match + if (sale_amount) { + price = sale_amount + } else { + price = amount + } + } else if (region_id === region._id) { + // If the region matches directly with the money amount this is the best + // price + if (sale_amount) { + price = sale_amount + } else { + price = amount + } + } } - }) + ) // Return the price if we found a suitable match if (typeof price !== "undefined") { @@ -268,9 +282,10 @@ class ProductVariantService extends BaseService { * @param {string} variantId - the id of the variant to update * @param {string} regionId - the id of the region to set price for * @param {number} amount - the amount to set the price to + * @param {number} saleAmount - the amount to set the price to * @return {Promise} the result of the update operation */ - async setRegionPrice(variantId, regionId, amount) { + async setRegionPrice(variantId, regionId, amount, saleAmount) { const variant = await this.retrieve(variantId) const region = await this.regionService_.retrieve(regionId) @@ -280,6 +295,7 @@ class ProductVariantService extends BaseService { const newPrices = variant.prices.map(moneyAmount => { if (moneyAmount.region_id === region._id) { moneyAmount.amount = amount + moneyAmount.sale_amount = amount foundRegion = true } @@ -291,6 +307,7 @@ class ProductVariantService extends BaseService { newPrices.push({ region_id: region._id, currency_code: region.currency_code, + sale_amount: saleAmount, amount, }) } @@ -327,10 +344,12 @@ class ProductVariantService extends BaseService { { region_id: region._id, currency_code: region.currency_code, + sale_amount: saleAmount, amount, }, { currency_code: region.currency_code, + sale_amount: saleAmount, amount, }, ], diff --git a/packages/medusa/src/services/product.js b/packages/medusa/src/services/product.js index faeb680997..b1b988a64e 100644 --- a/packages/medusa/src/services/product.js +++ b/packages/medusa/src/services/product.js @@ -178,13 +178,15 @@ class ProductService extends BaseService { await this.productVariantService_.setRegionPrice( variant._id, price.region_id, - price.amount + price.amount, + price.sale_amount || undefined ) } else { await this.productVariantService_.setCurrencyPrice( variant._id, price.currency_code, - price.amount + price.amount, + price.sale_amount || undefined ) } }