fix: connect inventory item to location before setting stock on product creation
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+16
-7
@@ -127,27 +127,36 @@ export async function createShopifyProduct(product: SyncProduct): Promise<string
|
||||
const variantId = String(data.product.variants[0].id)
|
||||
|
||||
// Récupérer le location_id par défaut pour setter le stock
|
||||
if (product.stock > 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`, {
|
||||
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: data.product.variants[0].inventory_item_id,
|
||||
available: product.stock,
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user