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

[Bug]: Issue completing cart with Custom Line Item #9995

Open
matt-rolley opened this issue Nov 8, 2024 · 12 comments
Open

[Bug]: Issue completing cart with Custom Line Item #9995

matt-rolley opened this issue Nov 8, 2024 · 12 comments

Comments

@matt-rolley
Copy link

matt-rolley commented Nov 8, 2024

Package.json file

{
  "name": "",
  "version": "0.0.1",
  "description": "",
  "license": "MIT",
  "keywords": [
    "sqlite",
    "postgres",
    "typescript",
    "ecommerce",
    "headless",
    "medusa"
  ],
  "engines": {
    "node": ">=20"
  },
  "scripts": {
    "build": "medusa build",
    "seed": "medusa exec ./src/scripts/seed.ts",
    "start": "medusa start",
    "dev": "medusa develop",
    "predeploy": "medusa db:migrate",
    "test:integration:http": "TEST_TYPE=integration:http NODE_OPTIONS=--experimental-vm-modules jest --silent=false --runInBand --forceExit",
    "test:integration:modules": "TEST_TYPE=integration:modules NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit",
    "test:unit": "TEST_TYPE=unit NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit"
  },
  "dependencies": {
    "@medusajs/admin-sdk": "2.0.2",
    "@medusajs/cli": "2.0.2",
    "@medusajs/framework": "2.0.2",
    "@medusajs/medusa": "2.0.2",
    "@mikro-orm/core": "5.9.7",
    "@mikro-orm/knex": "5.9.7",
    "@mikro-orm/migrations": "5.9.7",
    "@mikro-orm/postgresql": "5.9.7",
    "awilix": "^8.0.1",
    "pg": "^8.13.0"
  },
  "devDependencies": {
    "@medusajs/test-utils": "2.0.2",
    "@mikro-orm/cli": "5.9.7",
    "@swc/core": "1.5.7",
    "@swc/jest": "^0.2.36",
    "@types/jest": "^29.5.13",
    "@types/node": "^20.0.0",
    "@types/react": "^18.3.2",
    "@types/react-dom": "^18.2.25",
    "jest": "^29.7.0",
    "prop-types": "^15.8.1",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "ts-node": "^10.9.2",
    "typescript": "^5.6.2",
    "vite": "^5.2.11"
  }
}

Node.js version

v20.11.1

Database and its version

PostgresSQL 16

Operating system name and version

Docker

Browser name

No response

What happended?

In the complete-cart workflow, the cart transforms the line items using prepareLineItemData but this requires a variant to be set.

Other times that prepareLineItemData are used, there seems to be a variant check first, then it uses a prepareCustomLineItemData instead.

There are a few places that only use prepareLineItemData without checking if a variant exists, so not sure if adding the prepareCustomLineItemData will solve a few other bugs?

Expected behavior

  1. Add a custom line item using cartModuleService.addLineItems
  2. Initiate payment session using store.payment.initiatePaymentSession
  3. Call store.cart.complete to complete the order in the admin panel

Actual behavior

  1. Add a custom line item using cartModuleService.addLineItems
  2. Initiate payment session using store.payment.initiatePaymentSession
  3. Call store.cart.complete to complete the order (FAILS: "Cannot read properties of undefined (reading 'product')")

Link to reproduction repo

TODO

@jkuzmanovik
Copy link

Any update on this?

@matt-rolley
Copy link
Author

I've got a bit more spare time now, would it be useful if I take a look at fixing this one? Don't know if it's already being worked on?

@olivermrbl
Copy link
Contributor

@matt-rolley, we'll be working on this shortly. You can expect it to be part of a release before the end of the year.

@olivermrbl
Copy link
Contributor

@matt-rolley, I got something working locally. Would you be able to take a snapshot version for a spin? I'd like to get some feedback in case there are cases, I am not considering.

@matt-rolley
Copy link
Author

@olivermrbl yeah sure, sounds great, happy to give it a try.

@olivermrbl
Copy link
Contributor

@matt-rolley, great. Bear in mind this is a rough first take.

You can install the following snapshot of the Medusa packages in your project:

@2.0.8-snapshot-20241128171721

@olivermrbl olivermrbl assigned olivermrbl and unassigned olivermrbl Nov 28, 2024
@olivermrbl
Copy link
Contributor

Hey @matt-rolley, let me know if you get a chance to try it out.

Here's a new snapshot.

@matt-rolley
Copy link
Author

Hey @olivermrbl, really sorry for the delay - I'll be able to look at it today/tomorrow and get back to you asap.

@matt-rolley
Copy link
Author

Hey @olivermrbl, yeah it looks like to be working now on payment complete, I've noticed an issue with custom line items and increasing quantity using /store/carts/cart_01JE6N4KHWKJNZ06ABN8PS50GA/line-items/cali_01JE6NYTZDS47D6B3GPXAP5ZSP

I get this error:

{
  message: 'ProductVariant id not found: ',
  name: 'Error',
  stack: 'Error: ProductVariant id not found: \n' +
    '    at RemoteJoiner.checkIfKeysExist (/Users/matt/Work/TKStairs/tk-stairs/node_modules/@medusajs/orchestration/src/joiner/remote-joiner.ts:556:13)\n' +
    '    at RemoteJoiner.fetchData (/Users/matt/Work/TKStairs/tk-stairs/node_modules/@medusajs/orchestration/src/joiner/remote-joiner.ts:493:10)\n' +
    '    at processTicksAndRejections (node:internal/process/task_queues:105:5)\n' +
    '    at async RemoteJoiner.query (/Users/matt/Work/TKStairs/tk-stairs/node_modules/@medusajs/orchestration/src/joiner/remote-joiner.ts:1230:22)\n' +
    '    at async RemoteQuery.query (/Users/matt/Work/TKStairs/tk-stairs/node_modules/@medusajs/modules-sdk/src/remote-query/remote-query.ts:289:12)\n' +
    '    at async Query.query (/Users/matt/Work/TKStairs/tk-stairs/node_modules/@medusajs/modules-sdk/src/remote-query/query.ts:126:12)\n' +
    '    at async Object.<anonymous> (/Users/matt/Work/TKStairs/tk-stairs/node_modules/@medusajs/core-flows/src/common/steps/use-remote-query.ts:171:22)\n' +
    '    at async Object.invoke (/Users/matt/Work/TKStairs/tk-stairs/node_modules/@medusajs/workflows-sdk/src/utils/composer/helpers/create-step-handler.ts:72:52)\n' +
    '    at async DistributedTransaction.handler (/Users/matt/Work/TKStairs/tk-stairs/node_modules/@medusajs/orchestration/src/workflow/workflow-manager.ts:214:16)\n' +
    '    at async stepHandler (/Users/matt/Work/TKStairs/tk-stairs/node_modules/@medusajs/orchestration/src/transaction/transaction-orchestrator.ts:827:20)\n' +
    '⮑ sat /Users/matt/Work/TKStairs/tk-stairs/node_modules/@medusajs/core-flows/dist/cart/workflows/update-line-item-in-cart.js: [update-line-item-in-cart -> use-remote-query (invoke)]',
  __isMedusaError: true,
  type: 'not_found',
  code: undefined,
  date: 2024-12-04T21:02:54.265Z
}

I think this is related to the #9863 but I can get round that using a manual endpoint for changing item quantity.

If this issue could be wrapped up in the this ticket, that'd be amazing - if not, I'm still happy this one is solved :)

Thanks for the help/fix!

@matt-rolley
Copy link
Author

matt-rolley commented Dec 6, 2024

Hey @olivermrbl, sorry as I've been testing it more, I'm also getting an issue if I try to add an additional item to the cart too:

{
  message: "Cannot read properties of undefined (reading 'find')",
  name: 'TypeError',
  stack: "TypeError: Cannot read properties of undefined (reading 'find')\n" +
    '    at /Users/matt/Work/TKStairs/tk-stairs/node_modules/@medusajs/core-flows/src/cart/workflows/refresh-cart-items.ts:78:34\n' +
}

Anything I can do to help, just let me know :)

@olivermrbl
Copy link
Contributor

@matt-rolley, thanks for the report. Will have a look at this over the weekend. I've been doing some clean-up today, so I'll have a fresh version ready for you soon!

@sradevski
Copy link
Member

@olivermrbl does #10408 close this ticket and #9863?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants