diff --git a/integration-tests/api/__tests__/store/cart.js b/integration-tests/api/__tests__/store/cart.js index 6bd49c222e..86dae5b93e 100644 --- a/integration-tests/api/__tests__/store/cart.js +++ b/integration-tests/api/__tests__/store/cart.js @@ -264,6 +264,44 @@ describe("/store/carts", () => { ]) }) + it("adds line item to cart time customer pricing", async () => { + const api = useApi() + + // customer with customer-group 5 + const authResponse = await api.post("/store/auth", { + email: "test5@email.com", + password: "test", + }) + + const [authCookie] = authResponse.headers["set-cookie"][0].split(";") + + // Add standard line item to cart + const response = await api + .post( + "/store/carts/test-cart/line-items", + { + variant_id: "test-variant-sale-customer", + quantity: 1, + }, + { + // withCredentials: true, + headers: { + Cookie: authCookie, + }, + } + ) + .catch((err) => console.log(err)) + + expect(response.data.cart.items).toEqual([ + expect.objectContaining({ + cart_id: "test-cart", + unit_price: 700, + variant_id: "test-variant-sale-customer", + quantity: 1, + }), + ]) + }) + it("adds line item with quantity to cart with quantity discount", async () => { const api = useApi() diff --git a/integration-tests/api/helpers/cart-seeder.js b/integration-tests/api/helpers/cart-seeder.js index 116a58709e..0970d5803e 100644 --- a/integration-tests/api/helpers/cart-seeder.js +++ b/integration-tests/api/helpers/cart-seeder.js @@ -72,6 +72,38 @@ module.exports = async (connection, data = {}) => { `UPDATE "country" SET region_id='test-region-multiple' WHERE iso_2 = 'dk'` ) + const customer5 = await manager.create(Customer, { + id: "test-customer-5", + email: "test5@email.com", + first_name: "John", + last_name: "Deere", + password_hash: + "c2NyeXB0AAEAAAABAAAAAVMdaddoGjwU1TafDLLlBKnOTQga7P2dbrfgf3fB+rCD/cJOMuGzAvRdKutbYkVpuJWTU39P7OpuWNkUVoEETOVLMJafbI8qs8Qx/7jMQXkN", // password matching "test" + has_account: true, + }) + await manager.save(customer5) + + const c_group_5 = await manager.create(CustomerGroup, { + id: "test-group-5", + name: "test-group-5", + }) + await manager.save(c_group_5) + + customer5.groups = [c_group_5] + await manager.save(customer5) + + const priceList_customer = await manager.create(PriceList, { + id: "pl_customer", + name: "VIP winter sale", + description: "Winter sale for VIP customers.", + type: "sale", + status: "active", + }) + + priceList_customer.customer_groups = [c_group_5] + + await manager.save(priceList_customer) + const freeRule = manager.create(DiscountRule, { id: "free-shipping-rule", description: "Free shipping rule", @@ -390,6 +422,19 @@ module.exports = async (connection, data = {}) => { ], }) + await manager.insert(ProductVariant, { + id: "test-variant-sale-customer", + title: "test variant", + product_id: "test-product", + inventory_quantity: 1000, + options: [ + { + option_id: "test-option", + value: "Size", + }, + ], + }) + await manager.insert(ProductVariant, { id: "test-variant", title: "test variant", @@ -437,9 +482,31 @@ module.exports = async (connection, data = {}) => { amount: 800, price_list_id: "pl_current", }) - await manager.save(ma_sale_1) + const ma_sale_customer = manager.create(MoneyAmount, { + variant_id: "test-variant-sale-customer", + currency_code: "usd", + amount: 1000, + }) + await manager.save(ma_sale_customer) + + const ma_sale_customer_1 = manager.create(MoneyAmount, { + variant_id: "test-variant-sale-customer", + currency_code: "usd", + amount: 700, + price_list_id: "pl_customer", + }) + await manager.save(ma_sale_customer_1) + + const ma_sale_customer_2 = manager.create(MoneyAmount, { + variant_id: "test-variant-sale-customer", + currency_code: "usd", + amount: 800, + price_list_id: "pl_current", + }) + await manager.save(ma_sale_customer_2) + const ma_quantity = manager.create(MoneyAmount, { variant_id: "test-variant-quantity", currency_code: "usd", diff --git a/packages/medusa/src/api/routes/store/carts/__tests__/create-cart.js b/packages/medusa/src/api/routes/store/carts/__tests__/create-cart.js index 0fa4ddc619..203bebcb51 100644 --- a/packages/medusa/src/api/routes/store/carts/__tests__/create-cart.js +++ b/packages/medusa/src/api/routes/store/carts/__tests__/create-cart.js @@ -101,12 +101,14 @@ describe("POST /store/carts", () => { expect(LineItemServiceMock.generate).toHaveBeenCalledWith( IdMap.getId("testVariant"), IdMap.getId("testRegion"), - 3 + 3, + { customer_id: undefined } ) expect(LineItemServiceMock.generate).toHaveBeenCalledWith( IdMap.getId("testVariant1"), IdMap.getId("testRegion"), - 1 + 1, + { customer_id: undefined } ) expect(CartServiceMock.addLineItem).toHaveBeenCalledTimes(2) }) diff --git a/packages/medusa/src/api/routes/store/carts/create-cart.ts b/packages/medusa/src/api/routes/store/carts/create-cart.ts index f940ad3b5e..02f8dde4bf 100644 --- a/packages/medusa/src/api/routes/store/carts/create-cart.ts +++ b/packages/medusa/src/api/routes/store/carts/create-cart.ts @@ -129,7 +129,9 @@ export default async (req, res) => { validated.items.map(async (i) => { const lineItem = await lineItemService .withTransaction(manager) - .generate(i.variant_id, regionId, i.quantity) + .generate(i.variant_id, regionId, i.quantity, { + customer_id: req.user?.customer_id, + }) await cartService .withTransaction(manager) .addLineItem(cart.id, lineItem) diff --git a/packages/medusa/src/api/routes/store/carts/create-line-item.ts b/packages/medusa/src/api/routes/store/carts/create-line-item.ts index d5e6324be2..0f6966bff0 100644 --- a/packages/medusa/src/api/routes/store/carts/create-line-item.ts +++ b/packages/medusa/src/api/routes/store/carts/create-line-item.ts @@ -30,6 +30,7 @@ import { validator } from "../../../../utils/validator" export default async (req, res) => { const { id } = req.params + const customerId = req.user?.customer_id const validated = await validator(StorePostCartsCartLineItemsReq, req.body) const manager: EntityManager = req.scope.resolve("manager") @@ -43,6 +44,7 @@ export default async (req, res) => { const line = await lineItemService .withTransaction(m) .generate(validated.variant_id, cart.region_id, validated.quantity, { + customer_id: customerId || cart.customer_id, metadata: validated.metadata, }) await txCartService.addLineItem(id, line) diff --git a/packages/medusa/src/services/line-item.js b/packages/medusa/src/services/line-item.js index 9ba0200e7f..732920bf33 100644 --- a/packages/medusa/src/services/line-item.js +++ b/packages/medusa/src/services/line-item.js @@ -168,7 +168,7 @@ class LineItemService extends BaseService { .getRegionPrice(variant.id, { regionId: region.id, quantity: quantity, - customer_id: undefined, + customer_id: config.customer_id, include_discount_prices: true, }) }