import type { CollectionBeforeChangeHook } from 'payload' type Props = { productsSlug: string variantOptionsSlug: string } export const variantsCollectionBeforeChange: (args: Props) => CollectionBeforeChangeHook = ({ productsSlug, variantOptionsSlug }) => async ({ data, req }) => { if (data?.options?.length && data.options.length > 0) { const titleArray: string[] = [] const productID = data.product const product = await req.payload.findByID({ id: productID, collection: productsSlug, depth: 0, overrideAccess: true, select: { title: true, variantTypes: true, }, }) if (product.title && typeof product.title === 'string') { titleArray.push(product.title) } for (const option of data.options) { const variantOption = await req.payload.findByID({ id: option, collection: variantOptionsSlug, depth: 0, overrideAccess: true, select: { label: true, }, }) if (!variantOption) { continue } if (variantOption.label && typeof variantOption.label === 'string') { titleArray.push(variantOption.label) } } data.title = titleArray.join(' — ') } return data }