diff --git a/packages/medusa-plugin-contentful/src/services/contentful.js b/packages/medusa-plugin-contentful/src/services/contentful.js index 58a3076719..9da90aa101 100644 --- a/packages/medusa-plugin-contentful/src/services/contentful.js +++ b/packages/medusa-plugin-contentful/src/services/contentful.js @@ -160,15 +160,19 @@ class ContentfulService extends BaseService { try { productEntry = await environment.getEntry(product._id) } catch (error) { + console.log(error) return this.createProductInContentful(product) } - const variantEntries = await this.getVariantEntries_(product.variants) + const variantEntries = await this.getVariantEntries_(product._id) const variantLinks = this.getVariantLinks_(variantEntries) productEntry.fields = _.assignIn(productEntry.fields, { title: { "en-US": product.title, }, + options: { + "en-US": product.options, + }, variants: { "en-US": variantLinks, }, @@ -219,6 +223,9 @@ class ContentfulService extends BaseService { sku: { "en-US": variant.sku, }, + options: { + "en-US": variant.options, + }, prices: { "en-US": variant.prices, }, 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 495e311aa1..505b6bbdc7 100644 --- a/packages/medusa/src/api/routes/admin/products/update-product.js +++ b/packages/medusa/src/api/routes/admin/products/update-product.js @@ -58,7 +58,7 @@ export default async (req, res) => { const product = await productService.update(oldProduct._id, value) const data = await productService.decorate( - newProduct, + product, [ "title", "description", diff --git a/packages/medusa/src/api/routes/store/carts/create-cart.js b/packages/medusa/src/api/routes/store/carts/create-cart.js index a091872287..389e37e79a 100644 --- a/packages/medusa/src/api/routes/store/carts/create-cart.js +++ b/packages/medusa/src/api/routes/store/carts/create-cart.js @@ -2,7 +2,7 @@ import { Validator, MedusaError } from "medusa-core-utils" export default async (req, res) => { const schema = Validator.object().keys({ - region_id: Validator.string().required(), + region_id: Validator.string(), items: Validator.array() .items({ variant_id: Validator.string().required(), @@ -19,7 +19,16 @@ export default async (req, res) => { try { const lineItemService = req.scope.resolve("lineItemService") const cartService = req.scope.resolve("cartService") - let cart = await cartService.create({ region_id: value.region_id }) + + // Add a default region if no region has been specified + let regionId = value.region_id + if (!value.region_id) { + const regionService = req.scope.resolve("regionService") + const regions = await regionService.list() + regionId = regions[0]._id + } + + let cart = await cartService.create({ region_id: regionId }) if (value.items) { await Promise.all( @@ -35,7 +44,7 @@ export default async (req, res) => { } cart = await cartService.retrieve(cart._id) - cart = await cartService.decorate(cart) + cart = await cartService.decorate(cart, [], ["region"]) res.status(200).json({ cart }) } catch (err) { throw err diff --git a/packages/medusa/src/api/routes/store/carts/get-cart.js b/packages/medusa/src/api/routes/store/carts/get-cart.js index 5ed3e4b3b3..a5b2f4f84e 100644 --- a/packages/medusa/src/api/routes/store/carts/get-cart.js +++ b/packages/medusa/src/api/routes/store/carts/get-cart.js @@ -3,7 +3,7 @@ export default async (req, res) => { try { const cartService = req.scope.resolve("cartService") let cart = await cartService.retrieve(id) - cart = await cartService.decorate(cart) + cart = await cartService.decorate(cart, [], ["region"]) res.json({ cart }) } catch (err) { throw err diff --git a/packages/medusa/src/services/cart.js b/packages/medusa/src/services/cart.js index 29d52c3d59..8eadf54049 100644 --- a/packages/medusa/src/services/cart.js +++ b/packages/medusa/src/services/cart.js @@ -227,6 +227,9 @@ class CartService extends BaseService { async decorate(cart, fields, expandFields = []) { const c = cart.toObject() c.total = await this.totalsService_.getTotal(cart) + if (expandFields.includes("region")) { + c.region = await this.regionService_.retrieve(cart.region_id) + } return c }