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:
+27
-18
@@ -127,27 +127,36 @@ export async function createShopifyProduct(product: SyncProduct): Promise<string
|
|||||||
const variantId = String(data.product.variants[0].id)
|
const variantId = String(data.product.variants[0].id)
|
||||||
|
|
||||||
// Récupérer le location_id par défaut pour setter le stock
|
// Récupérer le location_id par défaut pour setter le stock
|
||||||
if (product.stock > 0) {
|
try {
|
||||||
try {
|
const locRes = await fetch(`${baseUrl}/locations.json`, { headers })
|
||||||
const locRes = await fetch(`${baseUrl}/locations.json`, { headers })
|
if (locRes.ok) {
|
||||||
if (locRes.ok) {
|
const locData = await locRes.json() as { locations: Array<{ id: number }> }
|
||||||
const locData = await locRes.json() as { locations: Array<{ id: number }> }
|
const locationId = locData.locations[0]?.id
|
||||||
const locationId = locData.locations[0]?.id
|
const inventoryItemId = data.product.variants[0].inventory_item_id
|
||||||
if (locationId) {
|
if (locationId && inventoryItemId) {
|
||||||
await fetch(`${baseUrl}/inventory_levels/set.json`, {
|
// Connect l'inventory item à la location avant de setter la quantité
|
||||||
method: 'POST',
|
await fetch(`${baseUrl}/inventory_levels/connect.json`, {
|
||||||
headers,
|
method: 'POST',
|
||||||
body: JSON.stringify({
|
headers,
|
||||||
location_id: locationId,
|
body: JSON.stringify({ location_id: locationId, inventory_item_id: inventoryItemId }),
|
||||||
inventory_item_id: data.product.variants[0].inventory_item_id,
|
})
|
||||||
available: product.stock,
|
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
|
void variantId
|
||||||
|
|||||||
Reference in New Issue
Block a user