private function runBulkAutoMapping($token, $db, &$messages)
{
// 1. Zwingender Direkttest der helloCash-API
$testUrl = "https://api.hellocash.business/api/v2/articles?limit=3&page=1";
$headers = array("Authorization: Bearer " . $token, "Accept: application/json");
$ch = curl_init($testUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode == 200 && !empty($response)) {
$data = json_decode($response, true);
$articles = isset($data['data']) ? $data['data'] : (is_array($data) ? $data : array());
if (!empty($articles)) {
$sample = array();
$limitCount = min(3, count($articles));
for ($i = 0; $i < $limitCount; $i++) {
$art = $articles[$i];
$sample[] = "ID: " . (isset($art['id']) ? $art['id'] : 'n/a') .
" | code: '" . (isset($art['code']) ? $art['code'] : 'leer') . "'" .
" | name: '" . (isset($art['name']) ? $art['name'] : 'leer') . "'";
}
$messages[] = "DEBUG HC DIREKT-TEST: " . implode(" || ", $sample);
}
}
// 2. Alle ungemappten HikaShop-Produkte laden
$query = $db->getQuery(true)
->select(array($db->quoteName('product_id'), $db->quoteName('product_code'), $db->quoteName('product_name')))
->from($db->quoteName('#__hikashop_product'))
->where($db->quoteName('product_id') . ' NOT IN (SELECT product_id FROM ' . $db->quoteName('#__hikashop_hellocash_map') . ')');
$db->setQuery($query);
$hikashopProducts = $db->loadObjectList();
if (empty($hikashopProducts)) {
return;
}
$codeMap = array();
$nameMap = array();
foreach ($hikashopProducts as $p) {
$cleanCode = strtolower(trim((string)$p->product_code));
if (!empty($cleanCode)) {
$codeMap[$cleanCode] = (int)$p->product_id;
}
// Saubere Namensnormalisierung (Umlaute ersetzen, Leerzeichen reduzieren)
$rawName = (string)$p->product_name;
$cleanName = strtolower(trim(preg_replace('/\s+/', ' ', $rawName)));
if (!empty($cleanName)) {
$nameMap[$cleanName] = array(
'id' => (int)$p->product_id,
'original' => $rawName
);
}
}
$mappedCount = 0;
$possibleFields = array('code', 'number', 'article_number', 'item_number', 'sku');
// 3. helloCash seitenweise scannen (bis zu 25 Seiten für >1500 Artikel)
for ($page = 1; $page <= 25; $page++) {
if (empty($codeMap) && empty($nameMap)) {
break;
}
$url = "https://api.hellocash.business/api/v2/articles?limit=100&page=" . $page;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode == 429) {
usleep(500000);
continue;
}
if ($httpCode != 200 || empty($response)) {
break;
}
$data = json_decode($response, true);
$articles = isset($data['data']) ? $data['data'] : (is_array($data) ? $data : array());
if (empty($articles)) {
break;
}
foreach ($articles as $art) {
$hcId = isset($art['id']) ? (int)$art['id'] : 0;
if ($hcId <= 0) continue;
$hikashopProductId = 0;
$matchType = '';
$matchedKey = '';
// A) Code-Matching über alle Felder
foreach ($possibleFields as $field) {
if (isset($art[$field])) {
$hcVal = strtolower(trim((string)$art[$field]));
if (!empty($hcVal) && isset($codeMap[$hcVal])) {
$hikashopProductId = $codeMap[$hcVal];
$matchType = 'Code';
$matchedKey = $hcVal;
break;
}
}
}
// B) Flexibles Namens-Matching (Exakt oder Teilstring)
if ($hikashopProductId <= 0 && isset($art['name'])) {
$hcName = strtolower(trim(preg_replace('/\s+/', ' ', (string)$art['name'])));
if (!empty($hcName)) {
foreach ($nameMap as $hkCleanName => $info) {
// Entweder exakter Treffer ODER der Name ist fast identisch / enthält sich
if ($hcName === $hkCleanName || levenshtein($hcName, $hkCleanName) <= 3) {
$hikashopProductId = $info['id'];
$matchType = 'Name';
$matchedKey = $hkCleanName;
break;
}
}
}
}
if ($hikashopProductId > 0) {
$insertQuery = "INSERT IGNORE INTO `#__hikashop_hellocash_map` (`product_id`, `hellocash_id`) VALUES (" . $hikashopProductId . ", " . $hcId . ")";
$db->setQuery($insertQuery);
try {
$db->execute();
$mappedCount++;
$messages[] = "AUTO-MAP ({$matchType}): HikaShop ID {$hikashopProductId} <-> helloCash ID {$hcId}";
// Aus Lookups entfernen
if ($matchType === 'Code') {
unset($codeMap[$matchedKey]);
} else {
unset($nameMap[$matchedKey]);
}
} catch (Exception $e) {}
}
}
if (count($articles) < 100) {
break;
}
usleep(100000);
}
if ($mappedCount > 0) {
$messages[] = "BULK-SCAN: {$mappedCount} neue Artikel erfolgreich verknüpft.";
}
}
Fehler: 404
Während der Anfrage ist ein Fehler aufgetreten.
Die Seite kann nicht angezeigt werden, weil:
- ein veraltetes Lesezeichen aufgerufen wurde.
- eine falsche Adresse aufgerufen wurde.
- über eine Suchmaschine ein veralteter Index dieser Website aufgerufen wurde.
- keine Zugriffsrechte für diese Seite vorhanden sind.
Zur Startseite wechseln
Startseite
Bei Problemen ist der Administrator dieser Website zuständig.
404 Page not found : en