Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

[plugin-ecommerce] Variant hooks call findByID without req, so variants can't be created in the same transaction as their product

Open
#18,338 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

  • #18344 by @fzozyurt — closed without merging

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
76/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
postgresql, typescript
Domain
backend, databases

Research direction

Read the variants beforeChange.ts and validateOptions.ts hooks, then compare the carts beforeChange hook and the other lookups mentioned in the issue. Run the provided transactional reproduction with the Postgres adapter; done means a product and its variants can be created in one transaction without NotFound.

Written by the indexing model from the issue text.

Description

Bug
Describe the Bug

In @payloadcms/plugin-ecommerce, two variant hooks look up the parent product with req.payload.findByID(...) without passing req, so the lookup runs outside the current transaction:

With a transactional adapter (Postgres), creating a product and its variants in the same transaction therefore fails: the product created earlier in the transaction isn't visible to these lookups, and the variant create throws NotFound. It works in the admin UI only because there the product is committed before variants are added.

Still present on main. The fix is to pass req to these findByID calls (other lookups in the same file, e.g. the variant options, have the same issue). The carts beforeChange hook that computes the subtotal (carts/beforeChange.ts) uses the same pattern.

Link to the code that reproduces this issue

https://github.com/payloadcms/payload/blob/v3.90.2/packages/plugin-ecommerce/src/collections/variants/createVariantsCollection/hooks/validateOptions.ts#L25

Reproduction Steps

With the ecommerce template (or any config using ecommercePlugin with variants enabled) on @payloadcms/db-postgres:

import { commitTransaction, createLocalReq, initTransaction } from 'payload'

const req = await createLocalReq({}, payload)
await initTransaction(req)

const product = await payload.create({
  collection: 'products',
  data: { title: 'Test', enableVariants: true, variantTypes: [variantTypeID] },
  req,
})

// Throws NotFound: the plugin's hooks read `products` outside the transaction
await payload.create({
  collection: 'variants',
  data: { product: product.id, options: [variantOptionID] },
  req,
})

await commitTransaction(req)

Expected: the variant is created. Actual: NotFound from validateOptions / the beforeChange hook.

Which area(s) are affected?

plugin: ecommerce, db: postgres

Environment Info
payload: 3.90.2
@payloadcms/plugin-ecommerce: 3.90.2
@payloadcms/db-postgres: 3.90.2
next: 16.3.3
Node: 22.23.3
pnpm: 11.27.1
OS: Linux x64
Dominant language
TypeScript
Stars
45.5k
Forks
4.3k
Avg merge
1d 19h
Merged PRs (30d)
295

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from payloadcms/payload

All issues in payloadcms/payload

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.