From 59bdd7d5cd7ae0995bd985ccd65fd5d189632255 Mon Sep 17 00:00:00 2001 From: Avatarsia Date: Tue, 24 Mar 2026 19:28:29 +0100 Subject: [PATCH 1/2] fix: use variation endpoint for category sync in WooCommerce product export Categories belong to the parent product in WooCommerce, not to variations. Use $category_product_id (parent ID for variants, product ID for simple products) for both GET and PUT category operations. Co-Authored-By: Claude Opus 4.6 (1M context) --- www/pages/shopimporter_woocommerce.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/www/pages/shopimporter_woocommerce.php b/www/pages/shopimporter_woocommerce.php index 0d221f317..392a749e7 100644 --- a/www/pages/shopimporter_woocommerce.php +++ b/www/pages/shopimporter_woocommerce.php @@ -726,9 +726,7 @@ public function ImportSendList() $this->logger->info("WooCommerce Variante geändert für Artikel: $nummer / Variation: $product_id (Parent: $parent_id)"); } else { // This is a regular product - $this->client->put('products/' . $product_id, array_merge([ - - ], $commonProductAtts)); + $this->client->put('products/' . $product_id, $commonProductAtts); $this->logger->info("WooCommerce Artikel geändert für Artikel: $nummer / $product_id"); } @@ -753,6 +751,8 @@ public function ImportSendList() // } // Update the associated product categories + // For variations, categories belong to the parent product + $category_product_id = ($remoteIdInformation['isvariant']) ? $remoteIdInformation['parent'] : $product_id; $chosenCats = array(); if (isset($tmp[$i]['kategorien']) || isset($tmp[$i]['kategoriename'])) { @@ -795,7 +795,7 @@ public function ImportSendList() if ($wcCatId) { // update category. We first retrieve the product and append the new product category, not replace the entire category array. - $alreadyAssignedWCCats = $this->client->get('products/' . $product_id, [ + $alreadyAssignedWCCats = $this->client->get('products/' . $category_product_id, [ 'per_page' => 1, ])->categories; @@ -814,7 +814,7 @@ public function ImportSendList() } // Update category assignment - $this->client->put('products/' . $product_id, [ + $this->client->put('products/' . $category_product_id, [ 'categories' => $allCatIdsWCAPIRep, ]); From 54b6fb6c55c29eb2db95c1275abee179623bbb41 Mon Sep 17 00:00:00 2001 From: Avatarsia Date: Tue, 24 Mar 2026 20:42:37 +0100 Subject: [PATCH 2/2] fix: handle stock sync for variable products by updating each variation Variable products (type=variable) in WooCommerce manage stock on the variation level, not the parent. When a parent product is detected, fetch all variations and set stock_quantity on each one individually. Also exposes product type from getShopIdBySKU() for downstream logic. Co-Authored-By: Claude Opus 4.6 (1M context) --- www/pages/shopimporter_woocommerce.php | 43 +++++++++++++++++++++----- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/www/pages/shopimporter_woocommerce.php b/www/pages/shopimporter_woocommerce.php index 392a749e7..d51295531 100644 --- a/www/pages/shopimporter_woocommerce.php +++ b/www/pages/shopimporter_woocommerce.php @@ -549,17 +549,43 @@ public function ImportSendListLager() // WooCommerce doesnt have a standard property for the other values, we're ignoring them ]; if ($remoteIdInformation['isvariant']) { + // This is a variation — update via parent/variations endpoint $result = $this->client->put('products/' . $remoteIdInformation['parent'] . '/variations/' . $remoteIdInformation['id'], $updateProductParams); + + $this->logger->error( + "WooCommerce Lagerzahlenübertragung für Variante: $nummer / $remoteIdInformation[id] (Parent: $remoteIdInformation[parent]) - Anzahl: $lageranzahl", + ['result' => $result] + ); + } elseif ($remoteIdInformation['type'] === 'variable') { + // This is a variable parent product — stock must be set on each variation individually + $variations = $this->client->get('products/' . $remoteIdInformation['id'] . '/variations', ['per_page' => 100]); + + if (!empty($variations)) { + foreach ($variations as $variation) { + $result = $this->client->put( + 'products/' . $remoteIdInformation['id'] . '/variations/' . $variation->id, + $updateProductParams + ); + + $this->logger->error( + "WooCommerce Lagerzahlenübertragung für Variante von variablem Produkt: $nummer / Variation: $variation->id (Parent: $remoteIdInformation[id]) - Anzahl: $lageranzahl", + ['result' => $result] + ); + } + } else { + $this->logger->error( + "WooCommerce variables Produkt $nummer / $remoteIdInformation[id] hat keine Variationen — Lager-Sync übersprungen" + ); + } } else { + // Simple product — direct update $result = $this->client->put('products/' . $remoteIdInformation['id'], $updateProductParams); - } - $this->logger->error( - "WooCommerce Lagerzahlenübertragung für Artikel: $nummer / $remoteIdInformation[id] - Anzahl: $lageranzahl", - [ - 'result' => $result - ] - ); + $this->logger->error( + "WooCommerce Lagerzahlenübertragung für Artikel: $nummer / $remoteIdInformation[id] - Anzahl: $lageranzahl", + ['result' => $result] + ); + } $anzahl++; } return $anzahl; @@ -1010,7 +1036,8 @@ private function getShopIdBySKU($sku) return [ 'id' => $product[0]->id, 'parent' => $product[0]->parent_id, - 'isvariant' => !empty($product[0]->parent_id) + 'isvariant' => !empty($product[0]->parent_id), + 'type' => $product[0]->type ?? 'simple' ]; }