diff --git a/src/app/api/creation/execute/route.ts b/src/app/api/creation/execute/route.ts index e41a994..4b1d98f 100644 --- a/src/app/api/creation/execute/route.ts +++ b/src/app/api/creation/execute/route.ts @@ -79,6 +79,7 @@ export async function POST(req: NextRequest) { description: longDesc, price: product.priceActual || '0.00', stock: product.stock, + weightKg: product.weightKg, status: 'active', }) if (product.collectionId) await addProductToCollection(shopifyId, product.collectionId) diff --git a/src/lib/shopifySync.ts b/src/lib/shopifySync.ts index 9a3613a..68152b0 100644 --- a/src/lib/shopifySync.ts +++ b/src/lib/shopifySync.ts @@ -91,13 +91,23 @@ async function fetchByStatus(status: 'active' | 'draft'): Promise { const { baseUrl, headers } = getConfig() + const variant: Record = { + price: product.price, + sku: product.ref, + inventory_management: 'shopify', + } + if (product.weightKg != null && product.weightKg > 0) { + variant.weight = product.weightKg + variant.weight_unit = 'kg' + } + const body = { product: { title: product.title, body_html: product.description, handle: product.handle, status: product.status, - variants: [{ price: product.price, sku: product.ref }], + variants: [variant], }, } @@ -113,7 +123,35 @@ export async function createShopifyProduct(product: SyncProduct): Promise 0) { + try { + const locRes = await fetch(`${baseUrl}/locations.json`, { headers }) + if (locRes.ok) { + const locData = await locRes.json() as { locations: Array<{ id: number }> } + const locationId = locData.locations[0]?.id + if (locationId) { + await fetch(`${baseUrl}/inventory_levels/set.json`, { + method: 'POST', + headers, + body: JSON.stringify({ + location_id: locationId, + inventory_item_id: data.product.variants[0].inventory_item_id, + available: product.stock, + }), + }) + } + } + } catch { + // Non bloquant : le produit est créé, le stock peut être corrigé manuellement + } + } + + void variantId + return shopifyId } /** diff --git a/src/types/sync.ts b/src/types/sync.ts index f365e80..dbda7a1 100644 --- a/src/types/sync.ts +++ b/src/types/sync.ts @@ -7,7 +7,8 @@ export interface SyncProduct { title: string // colonne B description: string // colonne C (texte brut, stocké en body_html) price: string // colonne D — "29.99" (2 décimales) - stock: number // colonne E — info affichage uniquement (non synchronisé) + stock: number // colonne E + weightKg?: number // poids en kg pour l'expédition status: 'active' | 'draft' // colonne F : "inactif"/"inactif" → draft, tout autre → active }