This commit is contained in:
dimitar 2025-02-04 18:24:30 +01:00
parent 7e0aa41be6
commit 3247eed766

View File

@ -74,23 +74,22 @@ class EcomZoneProductSync
try { try {
do { do {
$catalog = $this->client->getCatalog($page, $perPage); $catalog = $this->client->getCatalog($page, $perPage);
$importedCount = 0; $importedCount = 0;
foreach ($catalog['data'] as $product) { foreach ($catalog['data'] as $product) {
if ($this->importSingleProduct($product)) { if ($this->importSingleProduct($product)) {
$importedCount++; $importedCount++;
} }
} }
$totalImported += $importedCount; $totalImported += $importedCount;
$totalAvailable = $catalog['total']; $totalAvailable = $catalog['total'];
$page++; $page++;
EcomZoneLogger::log("Imported page $page", 'INFO', [ EcomZoneLogger::log("Imported page $page", 'INFO', [
'total_imported' => $totalImported, 'total_imported' => $totalImported,
'page' => $page, 'page' => $page,
'total_available' => $totalAvailable 'total_available' => $totalAvailable
]); ]);
} while ($page <= ceil($totalAvailable / $perPage) && isset($catalog['next_page_url']) && $catalog['next_page_url'] !== null); } while ($page <= ceil($totalAvailable / $perPage) && isset($catalog['next_page_url']) && $catalog['next_page_url'] !== null);
EcomZoneLogger::log("Finished importing products", 'INFO', [ EcomZoneLogger::log("Finished importing products", 'INFO', [
@ -109,7 +108,6 @@ EcomZoneLogger::log("Imported page $page", 'INFO', [
'imported' => $totalImported, 'imported' => $totalImported,
'total' => $totalAvailable 'total' => $totalAvailable
]; ];
} catch (Exception $e) { } catch (Exception $e) {
EcomZoneLogger::log("Error importing products: " . $e->getMessage(), 'ERROR'); EcomZoneLogger::log("Error importing products: " . $e->getMessage(), 'ERROR');
throw $e; throw $e;
@ -161,9 +159,9 @@ EcomZoneLogger::log("Imported page $page", 'INFO', [
} else { } else {
$product->update(); $product->update();
} }
StockAvailable::setQuantity($product->id, 0, (int)$data['stock']); StockAvailable::setQuantity($product->id, 0, (int)$data['stock']);
$product->available_for_order = true; // Ensure product is available for order $product->available_for_order = true; // Ensure product is available for order
$product->show_price = true; // Ensure price is shown $product->show_price = true; // Ensure price is shown
// Handle image import if URL is provided // Handle image import if URL is provided
if (isset($data['image']) && !empty($data['image'])) { if (isset($data['image']) && !empty($data['image'])) {
@ -179,7 +177,6 @@ $product->show_price = true; // Ensure price is shown
]); ]);
return true; return true;
} catch (Exception $e) { } catch (Exception $e) {
EcomZoneLogger::log("Error importing product", 'ERROR', [ EcomZoneLogger::log("Error importing product", 'ERROR', [
'sku' => $data['sku'], 'sku' => $data['sku'],
@ -191,8 +188,8 @@ $product->show_price = true; // Ensure price is shown
private function importProductImage($product, $imageUrl) private function importProductImage($product, $imageUrl)
{ {
try { try {
// Create temporary file // Create temporary file
$tmpFile = tempnam(_PS_TMP_IMG_DIR_, 'ecomzone_'); $tmpFile = tempnam(_PS_TMP_IMG_DIR_, 'ecomzone_');
@ -251,7 +248,6 @@ private function importProductImage($product, $imageUrl)
'sku' => $product->reference, 'sku' => $product->reference,
'image' => $imageUrl 'image' => $imageUrl
]); ]);
} catch (Exception $e) { } catch (Exception $e) {
EcomZoneLogger::log("Error importing product image", 'ERROR', [ EcomZoneLogger::log("Error importing product image", 'ERROR', [
'sku' => $product->reference, 'sku' => $product->reference,
@ -259,12 +255,12 @@ private function importProductImage($product, $imageUrl)
'error' => $e->getMessage() 'error' => $e->getMessage()
]); ]);
} }
} }
private function createDirectoryIfNotExists($directory) private function createDirectoryIfNotExists($directory)
{ {
if (!is_dir($directory)) { if (!is_dir($directory)) {
mkdir($directory, 0755, true); mkdir($directory, 0755, true);
} }
} }
} }