← All changes
|
includes/Services/Products/CreateSingleProductService.php
+53
-23
1.10.10
→
1.10.20
View file →
| @@ -39,28 +39,47 @@ | ||
| 39 | 39 | $vendorId = syncBasalamSettings()->getSettings(SettingsConfig::VENDOR_ID); |
| 40 | 40 | |
| 41 | 41 | $url = sprintf(Endpoints::PRODUCT_CREATE, $vendorId); |
| 42 | 42 | |
| 43 | - $maxDescriptionRetries = 3; | |
| 44 | - $descriptionRetry = 0; | |
| 43 | + $maxDescriptionRetries = 3; | |
| 44 | + $descriptionRetry = 0; | |
| 45 | + $skuRetry = false; | |
| 46 | + | |
| 47 | + while (true) { | |
| 48 | + try { | |
| 49 | + $request = $this->apiservice->post($url, $productData); | |
| 50 | + } catch (RetryableException $e) { | |
| 51 | + if ($this->retryWithoutDuplicateSku($productData, $e, $skuRetry)) { | |
| 52 | + continue; | |
| 53 | + } | |
| 54 | + | |
| 55 | + throw $e; | |
| 56 | + } catch (NonRetryableException $e) { | |
| 57 | + if ($this->retryWithoutDuplicateSku($productData, $e, $skuRetry)) { | |
| 58 | + continue; | |
| 59 | + } | |
| 60 | + | |
| 61 | + if ($descriptionRetry < $maxDescriptionRetries && $this->stripForbiddenDescription($e, $productData, $productId, $descriptionRetry)) { | |
| 62 | + $descriptionRetry++; | |
| 63 | + continue; | |
| 64 | + } | |
| 65 | + throw $e; | |
| 66 | + } catch (\Exception $e) { | |
| 67 | + if ($this->retryWithoutDuplicateSku($productData, $e, $skuRetry)) { | |
| 68 | + continue; | |
| 69 | + } | |
| 70 | + | |
| 71 | + throw new \Exception(esc_html($e->getMessage())); | |
| 72 | + } | |
| 73 | + | |
| 74 | + // Some API adapters return a non-2xx response instead of throwing it. | |
| 75 | + if ($this->retryWithoutDuplicateSku($productData, $request, $skuRetry)) { | |
| 76 | + continue; | |
| 77 | + } | |
| 78 | + | |
| 79 | + break; | |
| 80 | + } | |
| 45 | 81 | |
| 46 | - while (true) { | |
| 47 | - try { | |
| 48 | - $request = $this->apiservice->post($url, $productData); | |
| 49 | - break; | |
| 50 | - } catch (RetryableException $e) { | |
| 51 | - throw $e; | |
| 52 | - } catch (NonRetryableException $e) { | |
| 53 | - if ($descriptionRetry < $maxDescriptionRetries && $this->stripForbiddenDescription($e, $productData, $productId, $descriptionRetry)) { | |
| 54 | - $descriptionRetry++; | |
| 55 | - continue; | |
| 56 | - } | |
| 57 | - throw $e; | |
| 58 | - } catch (\Exception $e) { | |
| 59 | - throw new \Exception(esc_html($e->getMessage())); | |
| 60 | - } | |
| 61 | - } | |
| 62 | - | |
| 63 | 82 | if ($request['status_code'] != 201 && isset($request['status_code'])) { |
| 64 | 83 | |
| 65 | 84 | $body = $request['body'] ?? ''; |
| 66 | 85 | |
| @@ -194,12 +213,23 @@ | ||
| 194 | 213 | if ($cleaned === $productData['description']) return false; |
| 195 | 214 | |
| 196 | 215 | $productData['description'] = $cleaned; |
| 197 | 216 | |
| 198 | - return true; | |
| 199 | - } | |
| 200 | - | |
| 201 | - private function ValidCreateProductData(array $productData, int $productId): void | |
| 217 | + return true; | |
| 218 | + } | |
| 219 | + | |
| 220 | + private function retryWithoutDuplicateSku(array &$productData, $error, bool &$retried): bool | |
| 221 | + { | |
| 222 | + if ($retried || !ProductSkuRetry::hasSku($productData)) return false; | |
| 223 | + if (!ProductSkuRetry::isDuplicateSkuError($error)) return false; | |
| 224 | + | |
| 225 | + $productData = ProductSkuRetry::withoutSkus($productData); | |
| 226 | + $retried = true; | |
| 227 | + | |
| 228 | + return true; | |
| 229 | + } | |
| 230 | + | |
| 231 | + private function ValidCreateProductData(array $productData, int $productId): void | |
| 202 | 232 | { |
| 203 | 233 | $validation = $this->validator->validate($productData, $productId); |
| 204 | 234 | |
| 205 | 235 | if (!$validation['valid']) { |