Skip to content

Commit

Permalink
feat: initial script version
Browse files Browse the repository at this point in the history
  • Loading branch information
fPolic committed Jan 10, 2025
1 parent 79bdc47 commit 27f2572
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions packages/medusa/src/scripts/migrate-product-shipping-profile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { ExecArgs } from "@medusajs/framework/types"
import { ContainerRegistrationKeys, Modules } from "@medusajs/framework/utils"

export default async function assignProductsToShippingProfile({
container,
}: ExecArgs) {
const logger = container.resolve(ContainerRegistrationKeys.LOGGER)
const link = container.resolve(ContainerRegistrationKeys.LINK)
const query = container.resolve(ContainerRegistrationKeys.QUERY)

const shippingProfiles = await query.graph({
entity: "shipping_profile",
fields: ["id", "name"],
})

if (!shippingProfiles.data.length) {
logger.error(
"No shipping profiles found, please create a default shipping profile first"
)
}

const products = await query.graph({
entity: "product",
fields: ["id"],
})

const shippingProfileId = (
shippingProfiles.data.find((p) =>
p.name.toLocaleLowerCase().includes("default")
) || shippingProfiles.data[0]
).id

const links = products.data.map((product) => ({
[Modules.PRODUCT]: {
product_id: product.id,
},
[Modules.FULFILLMENT]: {
shipping_profile_id: shippingProfileId,
},
}))

await link.create(links)

logger.info("Products assigned to shipping profile")
}

0 comments on commit 27f2572

Please sign in to comment.