← All changes
|
includes/Services/Products/CreateSingleProductService.php
+61
-28
1.10.17
→
1.10.28
View file →
| @@ -1,9 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace SyncBasalam\Services\Products; |
| 4 | 4 | |
| 5 | -use SyncBasalam\Admin\Product\Data\Validators\CreateProductDataValidator; | |
| 5 | +use SyncBasalam\Admin\Product\Data\Validators\CreateProductDataValidator; | |
| 6 | +use SyncBasalam\Admin\Product\Data\Services\VariantService; | |
| 6 | 7 | use SyncBasalam\Admin\Settings\SettingsConfig; |
| 7 | 8 | use SyncBasalam\Config\Endpoints; |
| 8 | 9 | use SyncBasalam\Services\ApiServiceManager; |
| 9 | 10 | use SyncBasalam\Jobs\Exceptions\RetryableException; |
| @@ -39,28 +40,47 @@ | ||
| 39 | 40 | $vendorId = syncBasalamSettings()->getSettings(SettingsConfig::VENDOR_ID); |
| 40 | 41 | |
| 41 | 42 | $url = sprintf(Endpoints::PRODUCT_CREATE, $vendorId); |
| 42 | 43 | |
| 43 | - $maxDescriptionRetries = 3; | |
| 44 | - $descriptionRetry = 0; | |
| 44 | + $maxDescriptionRetries = 3; | |
| 45 | + $descriptionRetry = 0; | |
| 46 | + $skuRetry = false; | |
| 47 | + | |
| 48 | + while (true) { | |
| 49 | + try { | |
| 50 | + $request = $this->apiservice->post($url, $productData); | |
| 51 | + } catch (RetryableException $e) { | |
| 52 | + if ($this->retryWithoutDuplicateSku($productData, $e, $skuRetry)) { | |
| 53 | + continue; | |
| 54 | + } | |
| 55 | + | |
| 56 | + throw $e; | |
| 57 | + } catch (NonRetryableException $e) { | |
| 58 | + if ($this->retryWithoutDuplicateSku($productData, $e, $skuRetry)) { | |
| 59 | + continue; | |
| 60 | + } | |
| 61 | + | |
| 62 | + if ($descriptionRetry < $maxDescriptionRetries && $this->stripForbiddenDescription($e, $productData, $productId, $descriptionRetry)) { | |
| 63 | + $descriptionRetry++; | |
| 64 | + continue; | |
| 65 | + } | |
| 66 | + throw $e; | |
| 67 | + } catch (\Exception $e) { | |
| 68 | + if ($this->retryWithoutDuplicateSku($productData, $e, $skuRetry)) { | |
| 69 | + continue; | |
| 70 | + } | |
| 71 | + | |
| 72 | + throw new \Exception(esc_html($e->getMessage())); | |
| 73 | + } | |
| 74 | + | |
| 75 | + // Some API adapters return a non-2xx response instead of throwing it. | |
| 76 | + if ($this->retryWithoutDuplicateSku($productData, $request, $skuRetry)) { | |
| 77 | + continue; | |
| 78 | + } | |
| 79 | + | |
| 80 | + break; | |
| 81 | + } | |
| 45 | 82 | |
| 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 | 83 | if ($request['status_code'] != 201 && isset($request['status_code'])) { |
| 64 | 84 | |
| 65 | 85 | $body = $request['body'] ?? ''; |
| 66 | 86 | |
| @@ -154,15 +174,17 @@ | ||
| 154 | 174 | } |
| 155 | 175 | } |
| 156 | 176 | |
| 157 | 177 | $variationMapping = []; |
| 158 | - foreach ($wcVariations as $key => $wcVarId) { | |
| 178 | + foreach ($wcVariations as $key => $wcVarId) { | |
| 159 | 179 | if (isset($syncBasalamVariations[$key])) { |
| 160 | 180 | $variationMapping[$wcVarId] = $syncBasalamVariations[$key]; |
| 161 | 181 | update_post_meta($wcVarId, 'sync_basalam_variation_id', $syncBasalamVariations[$key]); |
| 162 | - } | |
| 163 | - } | |
| 164 | - } | |
| 182 | + } | |
| 183 | + } | |
| 184 | + | |
| 185 | + (new VariantService())->syncExpandedVariationIds($product, $responseData['variants']); | |
| 186 | + } | |
| 165 | 187 | } |
| 166 | 188 | |
| 167 | 189 | update_post_meta($productId, ProductMetaKey::basalamProductId(), $responseData['id']); |
| 168 | 190 | update_post_meta($productId, ProductMetaKey::basalamProductStatus(), 2976); |
| @@ -194,12 +216,23 @@ | ||
| 194 | 216 | if ($cleaned === $productData['description']) return false; |
| 195 | 217 | |
| 196 | 218 | $productData['description'] = $cleaned; |
| 197 | 219 | |
| 198 | - return true; | |
| 199 | - } | |
| 200 | - | |
| 201 | - private function ValidCreateProductData(array $productData, int $productId): void | |
| 220 | + return true; | |
| 221 | + } | |
| 222 | + | |
| 223 | + private function retryWithoutDuplicateSku(array &$productData, $error, bool &$retried): bool | |
| 224 | + { | |
| 225 | + if ($retried || !ProductSkuRetry::hasSku($productData)) return false; | |
| 226 | + if (!ProductSkuRetry::isDuplicateSkuError($error)) return false; | |
| 227 | + | |
| 228 | + $productData = ProductSkuRetry::withoutSkus($productData); | |
| 229 | + $retried = true; | |
| 230 | + | |
| 231 | + return true; | |
| 232 | + } | |
| 233 | + | |
| 234 | + private function ValidCreateProductData(array $productData, int $productId): void | |
| 202 | 235 | { |
| 203 | 236 | $validation = $this->validator->validate($productData, $productId); |
| 204 | 237 | |
| 205 | 238 | if (!$validation['valid']) { |