PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / 1.10.28
ووسلام – همگام سازی ووکامرس و باسلام v1.10.28
1.10.28 1.1.26 1.10.27 1.10.24 1.10.25 1.10.23 1.10.22 1.10.21 1.10.19 1.10.20 1.10.18 1.10.17 1.10.15 1.10.14 1.10.13 1.10.12 1.10.10 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 All 61 releases
← All changes | includes/Services/Products/CreateSingleProductService.php +71 -32 1.10.6 → 1.10.28 View file →
@@ -1,15 +1,17 @@
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;
10 11 use SyncBasalam\Jobs\Exceptions\NonRetryableException;
11 -use SyncBasalam\Utilities\ProductMetaKey;
12 +use SyncBasalam\Utilities\ProductMetaKey;
13 +use SyncBasalam\Services\VendorSyncPolicy;
12 14
13 15 defined('ABSPATH') || exit;
14 16
15 17 class CreateSingleProductService
@@ -22,11 +24,16 @@
22 24 $this->apiservice = syncBasalamContainer()->get(ApiServiceManager::class);
23 25 $this->validator = $validator ?: new CreateProductDataValidator();
24 26 }
25 27
26 - public function createProductInBasalam($productData, $productId)
27 - {
28 - $productData = apply_filters('sync_basalam_product_data_before_create', $productData, $productId);
28 + public function createProductInBasalam($productData, $productId)
29 + {
30 + $vendorSyncPolicy = syncBasalamContainer()->get(VendorSyncPolicy::class);
31 + if (!$vendorSyncPolicy->canCreate()) {
32 + throw NonRetryableException::invalidData($vendorSyncPolicy->getRestrictionMessage(false));
33 + }
34 +
35 + $productData = apply_filters('sync_basalam_product_data_before_create', $productData, $productId);
29 36 $this->ValidCreateProductData($productData, $productId);
30 37
31 38 do_action('sync_basalam_before_create_product_api', $productId, $productData);
32 39
@@ -33,28 +40,47 @@
33 40 $vendorId = syncBasalamSettings()->getSettings(SettingsConfig::VENDOR_ID);
34 41
35 42 $url = sprintf(Endpoints::PRODUCT_CREATE, $vendorId);
36 43
37 - $maxDescriptionRetries = 3;
38 - $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 + }
39 82
40 - while (true) {
41 - try {
42 - $request = $this->apiservice->post($url, $productData);
43 - break;
44 - } catch (RetryableException $e) {
45 - throw $e;
46 - } catch (NonRetryableException $e) {
47 - if ($descriptionRetry < $maxDescriptionRetries && $this->stripForbiddenDescription($e, $productData, $productId, $descriptionRetry)) {
48 - $descriptionRetry++;
49 - continue;
50 - }
51 - throw $e;
52 - } catch (\Exception $e) {
53 - throw new \Exception(esc_html($e->getMessage()));
54 - }
55 - }
56 -
57 83 if ($request['status_code'] != 201 && isset($request['status_code'])) {
58 84
59 85 $body = $request['body'] ?? '';
60 86
@@ -148,15 +174,17 @@
148 174 }
149 175 }
150 176
151 177 $variationMapping = [];
152 - foreach ($wcVariations as $key => $wcVarId) {
178 + foreach ($wcVariations as $key => $wcVarId) {
153 179 if (isset($syncBasalamVariations[$key])) {
154 180 $variationMapping[$wcVarId] = $syncBasalamVariations[$key];
155 181 update_post_meta($wcVarId, 'sync_basalam_variation_id', $syncBasalamVariations[$key]);
156 - }
157 - }
158 - }
182 + }
183 + }
184 +
185 + (new VariantService())->syncExpandedVariationIds($product, $responseData['variants']);
186 + }
159 187 }
160 188
161 189 update_post_meta($productId, ProductMetaKey::basalamProductId(), $responseData['id']);
162 190 update_post_meta($productId, ProductMetaKey::basalamProductStatus(), 2976);
@@ -188,12 +216,23 @@
188 216 if ($cleaned === $productData['description']) return false;
189 217
190 218 $productData['description'] = $cleaned;
191 219
192 - return true;
193 - }
194 -
195 - 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
196 235 {
197 236 $validation = $this->validator->validate($productData, $productId);
198 237
199 238 if (!$validation['valid']) {