createProductVariantsWorkflow - Medusa Core Workflows Reference
This documentation provides a reference to the createProductVariantsWorkflow
. It belongs to the @medusajs/medusa/core-flows
package.
This workflow creates one or more product variants. It's used by the Create Product Variant Admin API Route.
This workflow has a hook that allows you to perform custom actions on the created product variants. For example, you can pass under additional_data
custom data that
allows you to create custom data models linked to the product variants.
You can also use this workflow within your customizations or your own custom workflows, allowing you to wrap custom logic around product-variant creation.
Note: Learn more about adding rules to the product variant's prices in the Pricing Module's
Price Rules documentation.Examples`import type {
MedusaRequest,
MedusaResponse,
} from "@medusajs/framework/http"
import { createProductVariantsWorkflow } from "@medusajs/medusa/core-flows"
export async function POST(
req: MedusaRequest,
res: MedusaResponse
) {
const { result } = await createProductVariantsWorkflow(req.scope)
.run({
input: {
product_variants: [{
product_id: "prod_123",
sku: "SHIRT-123",
title: "Small Shirt",
prices: [{
amount: 10,
currency_code: "USD",
}, ],
options: {
Size: "Small",
},
}, ],
additional_data: {
erp_id: "123"
}
}
})
res.send(result)
}
``import {
type SubscriberConfig,
type SubscriberArgs,
} from "@medusajs/framework"
import { createProductVariantsWorkflow } from "@medusajs/medusa/core-flows"
export default async function handleOrderPlaced({
event: { data },
container,
}: SubscriberArgs < { id: string } > ) {
const { result } = await createProductVariantsWorkflow(container)
.run({
input: {
product_variants: [{
product_id: "prod_123",
sku: "SHIRT-123",
title: "Small Shirt",
prices: [{
amount: 10,
currency_code: "USD",
}, ],
options: {
Size: "Small",
},
}, ],
additional_data: {
erp_id: "123"
}
}
})
console.log(result)
}
export const config: SubscriberConfig = {
event: "order.placed",
}
``import { MedusaContainer } from "@medusajs/framework/types"
import { createProductVariantsWorkflow } from "@medusajs/medusa/core-flows"
export default async function myCustomJob(
container: MedusaContainer
) {
const { result } = await createProductVariantsWorkflow(container)
.run({
input: {
product_variants: [{
product_id: "prod_123",
sku: "SHIRT-123",
title: "Small Shirt",
prices: [{
amount: 10,
currency_code: "USD",
}, ],
options: {
Size: "Small",
},
}, ],
additional_data: {
erp_id: "123"
}
}
})
console.log(result)
}
export const config = {
name: "run-once-a-day",
schedule: "0 0 * * *",
}
``import { createWorkflow } from "@medusajs/framework/workflows-sdk"
import { createProductVariantsWorkflow } from "@medusajs/medusa/core-flows"
const myWorkflow = createWorkflow(
"my-workflow",
() => {
const result = createProductVariantsWorkflow
.runAsStep({
input: {
product_variants: [{
product_id: "prod_123",
sku: "SHIRT-123",
title: "Small Shirt",
prices: [{
amount: 10,
currency_code: "USD",
}, ],
options: {
Size: "Small",
},
}, ],
additional_data: {
erp_id: "123"
}
}
})
}
)
`StepsInputOutputHooksHooks allow you to inject custom functionalities into the workflow. You'll receive data from the workflow, as well as additional data sent through an HTTP request.Learn more about Hooks and Additional Data.productVariantsCreatedHandlers consuming this hook accept the following input.
Was this page helpful?