← All changes
|
includes/Services/Products/UpdateProductVariationsService.php
+27
-8
1.10.20
→
1.10.21
View file →
| @@ -4,8 +4,9 @@ | ||
| 4 | 4 | |
| 5 | 5 | use SyncBasalam\Config\Endpoints; |
| 6 | 6 | use SyncBasalam\Jobs\Exceptions\NonRetryableException; |
| 7 | 7 | use SyncBasalam\Jobs\Exceptions\RetryableException; |
| 8 | +use SyncBasalam\Jobs\Exceptions\StaleVariationException; | |
| 8 | 9 | use SyncBasalam\Logger\Logger; |
| 9 | 10 | use SyncBasalam\Services\ApiServiceManager; |
| 10 | 11 | |
| 11 | 12 | defined('ABSPATH') || exit; |
| @@ -16,11 +17,11 @@ | ||
| 16 | 17 | private const VARIATION_FIELDS = ['primary_price', 'stock', 'sku']; |
| 17 | 18 | |
| 18 | 19 | private $apiservice; |
| 19 | 20 | |
| 20 | - public function __construct() | |
| 21 | + public function __construct($apiservice = null) | |
| 21 | 22 | { |
| 22 | - $this->apiservice = syncBasalamContainer()->get(ApiServiceManager::class); | |
| 23 | + $this->apiservice = $apiservice ?: syncBasalamContainer()->get(ApiServiceManager::class); | |
| 23 | 24 | } |
| 24 | 25 | |
| 25 | 26 | /** |
| 26 | 27 | * True when every variant already has a Basalam variation id, so each one can be |
| @@ -64,18 +65,26 @@ | ||
| 64 | 65 | $this->sendVariationUpdate($url, $data); |
| 65 | 66 | $updated++; |
| 66 | 67 | } catch (RetryableException $e) { |
| 67 | 68 | throw $e; |
| 69 | + } catch (NonRetryableException $e) { | |
| 70 | + // A missing remote variation means the local mapping is stale, | |
| 71 | + // not that the whole product is missing. Abort this per-variant | |
| 72 | + // pass immediately so the caller can rebuild all mappings in a | |
| 73 | + // single product update. | |
| 74 | + if ((int) $e->getCode() === 404) { | |
| 75 | + throw new StaleVariationException($basalamProductId, $variant['id'], $e); | |
| 76 | + } | |
| 77 | + | |
| 78 | + $failed++; | |
| 79 | + $firstError = $firstError ?: $e->getMessage(); | |
| 80 | + | |
| 81 | + $this->logVariationError($e, $productId, $basalamProductId, $variant['id']); | |
| 68 | 82 | } catch (\Exception $e) { |
| 69 | 83 | $failed++; |
| 70 | 84 | $firstError = $firstError ?: $e->getMessage(); |
| 71 | 85 | |
| 72 | - Logger::error('خطا در بروزرسانی متغیر محصول در باسلام: ' . $e->getMessage(), [ | |
| 73 | - 'operation' => 'بروزرسانی متغیر محصول', | |
| 74 | - 'product_id' => $productId, | |
| 75 | - 'basalam_product_id' => $basalamProductId, | |
| 76 | - 'basalam_variation_id' => $variant['id'], | |
| 77 | - ]); | |
| 86 | + $this->logVariationError($e, $productId, $basalamProductId, $variant['id']); | |
| 78 | 87 | } |
| 79 | 88 | } |
| 80 | 89 | |
| 81 | 90 | // Every variation failed with a permanent error: report it instead of pretending success. |
| @@ -83,8 +92,18 @@ | ||
| 83 | 92 | throw NonRetryableException::permanent(esc_html('بروزرسانی متغیرهای محصول ناموفق بود: ' . $firstError)); |
| 84 | 93 | } |
| 85 | 94 | |
| 86 | 95 | return ['updated' => $updated, 'skipped' => $skipped, 'failed' => $failed]; |
| 96 | + } | |
| 97 | + | |
| 98 | + private function logVariationError(\Throwable $error, $productId, $basalamProductId, $basalamVariationId): void | |
| 99 | + { | |
| 100 | + Logger::error('خطا در بروزرسانی متغیر محصول در باسلام: ' . $error->getMessage(), [ | |
| 101 | + 'operation' => 'بروزرسانی متغیر محصول', | |
| 102 | + 'product_id' => $productId, | |
| 103 | + 'basalam_product_id' => $basalamProductId, | |
| 104 | + 'basalam_variation_id' => $basalamVariationId, | |
| 105 | + ]); | |
| 87 | 106 | } |
| 88 | 107 | |
| 89 | 108 | private function sendVariationUpdate(string $url, array $data): void |
| 90 | 109 | { |