diff --git a/integration-tests/http/__tests__/product/admin/product.spec.ts b/integration-tests/http/__tests__/product/admin/product.spec.ts index 34169302b595a..912d085f45097 100644 --- a/integration-tests/http/__tests__/product/admin/product.spec.ts +++ b/integration-tests/http/__tests__/product/admin/product.spec.ts @@ -1528,6 +1528,63 @@ medusaIntegrationTestRunner({ ) }) + it("updates products with shipping profiles", async () => { + const shippingProfile2 = ( + await api.post( + `/admin/shipping-profiles`, + { name: "heavy", type: "heavy" }, + adminHeaders + ) + ).data.shipping_profile + + let fetchProduct = await api.get( + `/admin/products/${baseProduct.id}?fields=+shipping_profile.id`, + adminHeaders + ) + + let payload: Record = { + shipping_profile_id: shippingProfile2.id, + } + + let response = await api + .post(`/admin/products/${baseProduct.id}`, payload, adminHeaders) + .catch((err) => { + console.log(err) + }) + + expect(response.status).toEqual(200) + + fetchProduct = await api.get( + `/admin/products/${baseProduct.id}?fields=+shipping_profile.id`, + adminHeaders + ) + + expect(fetchProduct.data.product.shipping_profile.id).toEqual( + shippingProfile2.id + ) + + payload = { + subtitle: "new subtitle", + } + + response = await api + .post(`/admin/products/${baseProduct.id}`, payload, adminHeaders) + .catch((err) => { + console.log(err) + }) + + expect(response.status).toEqual(200) + + fetchProduct = await api.get( + `/admin/products/${baseProduct.id}?fields=+shipping_profile.id`, + adminHeaders + ) + + expect(fetchProduct.data.product.shipping_profile.id).toEqual( + shippingProfile2.id + ) + }) + it("updates a product (update prices, tags, update status, delete collection, delete type, replaces images)", async () => { const payload = { collection_id: null, diff --git a/packages/admin/dashboard/src/routes/products/product-detail/components/product-shipping-profile-section/product-shipping-profile-section.tsx b/packages/admin/dashboard/src/routes/products/product-detail/components/product-shipping-profile-section/product-shipping-profile-section.tsx index 23bead55d7bb9..d6f352345662a 100644 --- a/packages/admin/dashboard/src/routes/products/product-detail/components/product-shipping-profile-section/product-shipping-profile-section.tsx +++ b/packages/admin/dashboard/src/routes/products/product-detail/components/product-shipping-profile-section/product-shipping-profile-section.tsx @@ -19,10 +19,6 @@ export const ProductShippingProfileSection = ({ const shippingProfile = product.shipping_profile - if (!shippingProfile) { - return null - } - return (
@@ -42,12 +38,14 @@ export const ProductShippingProfileSection = ({ />
- } - /> + {shippingProfile && ( + } + /> + )}
) } diff --git a/packages/core/core-flows/src/product/workflows/update-products.ts b/packages/core/core-flows/src/product/workflows/update-products.ts index ae5ece69f9958..4e8c582ae88dd 100644 --- a/packages/core/core-flows/src/product/workflows/update-products.ts +++ b/packages/core/core-flows/src/product/workflows/update-products.ts @@ -141,6 +141,25 @@ function findProductsWithSalesChannels({ return !input.update?.sales_channels ? [] : productIds } +function findProductsWithShippingProfiles({ + updatedProducts, + input, +}: { + updatedProducts: ProductTypes.ProductDTO[] + input: UpdateProductWorkflowInput +}) { + let productIds = updatedProducts.map((p) => p.id) + + if ("products" in input) { + const discardedProductIds: string[] = input.products + .filter((p) => !p.shipping_profile_id) + .map((p) => p.id as string) + return arrayDifference(productIds, discardedProductIds) + } + + return !input.update?.shipping_profile_id ? [] : productIds +} + function prepareSalesChannelLinks({ input, updatedProducts, @@ -417,10 +436,6 @@ export const updateProductsWorkflow = createWorkflow( const toUpdateInput = transform({ input }, prepareUpdateProductInput) const updatedProducts = updateProductsStep(toUpdateInput) - const updatedPorductIds = transform({ updatedProducts }, (data) => { - return data.updatedProducts.map((p) => p.id) - }) - const salesChannelLinks = transform( { input, updatedProducts }, prepareSalesChannelLinks @@ -441,6 +456,11 @@ export const updateProductsWorkflow = createWorkflow( findProductsWithSalesChannels ) + const productsWithShippingProfiles = transform( + { updatedProducts, input }, + findProductsWithShippingProfiles + ) + const currentSalesChannelLinks = useRemoteQueryStep({ entry_point: "product_sales_channel", fields: ["product_id", "sales_channel_id"], @@ -450,7 +470,7 @@ export const updateProductsWorkflow = createWorkflow( const currentShippingProfileLinks = useRemoteQueryStep({ entry_point: "product_shipping_profile", fields: ["product_id", "shipping_profile_id"], - variables: { filters: { product_id: updatedPorductIds } }, + variables: { filters: { product_id: productsWithShippingProfiles } }, }).config({ name: "get-current-shipping-profile-links-step" }) const toDeleteSalesChannelLinks = transform(