← All changes
|
includes/Services/Products/UpdateProductVariationsService.php
+42
-2
1.10.8
→
1.10.20
View file →
| @@ -12,9 +12,9 @@ | ||
| 12 | 12 | |
| 13 | 13 | class UpdateProductVariationsService |
| 14 | 14 | { |
| 15 | 15 | /** Variant fields that are sent to the single-variation endpoint. */ |
| 16 | - private const VARIATION_FIELDS = ['primary_price', 'stock']; | |
| 16 | + private const VARIATION_FIELDS = ['primary_price', 'stock', 'sku']; | |
| 17 | 17 | |
| 18 | 18 | private $apiservice; |
| 19 | 19 | |
| 20 | 20 | public function __construct() |
| @@ -60,9 +60,9 @@ | ||
| 60 | 60 | |
| 61 | 61 | $url = sprintf(Endpoints::PRODUCT_VARIATION_UPDATE, $basalamProductId, $variant['id']); |
| 62 | 62 | |
| 63 | 63 | try { |
| 64 | - $this->apiservice->patch($url, $data); | |
| 64 | + $this->sendVariationUpdate($url, $data); | |
| 65 | 65 | $updated++; |
| 66 | 66 | } catch (RetryableException $e) { |
| 67 | 67 | throw $e; |
| 68 | 68 | } catch (\Exception $e) { |
| @@ -83,6 +83,46 @@ | ||
| 83 | 83 | throw NonRetryableException::permanent(esc_html('بروزرسانی متغیرهای محصول ناموفق بود: ' . $firstError)); |
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | return ['updated' => $updated, 'skipped' => $skipped, 'failed' => $failed]; |
| 87 | + } | |
| 88 | + | |
| 89 | + private function sendVariationUpdate(string $url, array $data): void | |
| 90 | + { | |
| 91 | + $skuRetry = false; | |
| 92 | + | |
| 93 | + while (true) { | |
| 94 | + try { | |
| 95 | + $response = $this->apiservice->patch($url, $data); | |
| 96 | + } catch (RetryableException $e) { | |
| 97 | + if ($this->retryWithoutDuplicateSku($data, $e, $skuRetry)) continue; | |
| 98 | + | |
| 99 | + throw $e; | |
| 100 | + } catch (\Exception $e) { | |
| 101 | + if ($this->retryWithoutDuplicateSku($data, $e, $skuRetry)) continue; | |
| 102 | + | |
| 103 | + throw $e; | |
| 104 | + } | |
| 105 | + | |
| 106 | + if ($this->retryWithoutDuplicateSku($data, $response, $skuRetry)) continue; | |
| 107 | + | |
| 108 | + // API adapters normally throw client errors, but keep the fallback | |
| 109 | + // bounded if an adapter returns the error response directly. | |
| 110 | + if (ProductSkuRetry::isDuplicateSkuError($response)) { | |
| 111 | + throw NonRetryableException::permanent('SKU متغیر محصول در باسلام تکراری است.'); | |
| 112 | + } | |
| 113 | + | |
| 114 | + return; | |
| 115 | + } | |
| 116 | + } | |
| 117 | + | |
| 118 | + private function retryWithoutDuplicateSku(array &$data, $error, bool &$retried): bool | |
| 119 | + { | |
| 120 | + if ($retried || !ProductSkuRetry::hasSku($data)) return false; | |
| 121 | + if (!ProductSkuRetry::isDuplicateSkuError($error)) return false; | |
| 122 | + | |
| 123 | + $data = ProductSkuRetry::withoutSkus($data); | |
| 124 | + $retried = true; | |
| 125 | + | |
| 126 | + return true; | |
| 87 | 127 | } |
| 88 | 128 | } |