diff --git a/src/lib/shopifySync.ts b/src/lib/shopifySync.ts index 68152b0..2acd386 100644 --- a/src/lib/shopifySync.ts +++ b/src/lib/shopifySync.ts @@ -127,27 +127,36 @@ 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, - }), - }) + 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 + const inventoryItemId = data.product.variants[0].inventory_item_id + if (locationId && inventoryItemId) { + // Connect l'inventory item à la location avant de setter la quantité + await fetch(`${baseUrl}/inventory_levels/connect.json`, { + method: 'POST', + headers, + body: JSON.stringify({ location_id: locationId, inventory_item_id: inventoryItemId }), + }) + const setRes = await fetch(`${baseUrl}/inventory_levels/set.json`, { + method: 'POST', + headers, + body: JSON.stringify({ + location_id: locationId, + inventory_item_id: inventoryItemId, + available: product.stock ?? 0, + }), + }) + if (!setRes.ok) { + const errText = await setRes.text() + console.error(`[shopify] inventory_levels/set failed: ${setRes.status} — ${errText}`) } } - } catch { - // Non bloquant : le produit est créé, le stock peut être corrigé manuellement } + } catch (err) { + console.error('[shopify] stock set error:', err) } void variantId