PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / 1.10.0
ووسلام – همگام سازی ووکامرس و باسلام v1.10.0
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 1.10.1 1.10.0 1.9.2 1.9.1 1.9.0 1.8.8 1.8.5 1.8.6 All 53 releases
sync-basalam / includes / Services / Products / CreateSingleProductService.php

CreateSingleProductService.php in ووسلام – همگام سازی ووکامرس و باسلام 1.10.0, at includes/Services/Products/CreateSingleProductService.php

192 lines 7.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SyncBasalam\Services\Products;
4
5 use SyncBasalam\Admin\Product\Data\Validators\CreateProductDataValidator;
6 use SyncBasalam\Admin\Settings\SettingsConfig;
7 use SyncBasalam\Config\Endpoints;
8 use SyncBasalam\Services\ApiServiceManager;
9 use SyncBasalam\Jobs\Exceptions\RetryableException;
10 use SyncBasalam\Jobs\Exceptions\NonRetryableException;
11 use SyncBasalam\Utilities\ProductMetaKey;
12
13 defined('ABSPATH') || exit;
14
15 class CreateSingleProductService
16 {
17 private $apiservice;
18 private $validator;
19
20 public function __construct($validator = null)
21 {
22 $this->apiservice = syncBasalamContainer()->get(ApiServiceManager::class);
23 $this->validator = $validator ?: new CreateProductDataValidator();
24 }
25
26 public function createProductInBasalam($productData, $productId)
27 {
28 $productData = apply_filters('sync_basalam_product_data_before_create', $productData, $productId);
29 $this->ValidCreateProductData($productData, $productId);
30
31 do_action('sync_basalam_before_create_product_api', $productId, $productData);
32
33 $vendorId = syncBasalamSettings()->getSettings(SettingsConfig::VENDOR_ID);
34
35 $url = sprintf(Endpoints::PRODUCT_CREATE, $vendorId);
36
37 try {
38 $request = $this->apiservice->post($url, $productData);
39 } catch (RetryableException $e) {
40 throw $e;
41 } catch (NonRetryableException $e) {
42 throw $e;
43 } catch (\Exception $e) {
44 throw new \Exception(esc_html($e->getMessage()));
45 }
46
47 if ($request['status_code'] != 201 && isset($request['status_code'])) {
48
49 $body = $request['body'] ?? '';
50
51 if (is_string($body))
52 $responseData = json_decode($body, true);
53 else $responseData = $body;
54
55 if (!is_array($responseData)) $responseData = [];
56
57 if ($responseData && isset($responseData['messages'][0])) {
58 $message = $responseData['messages'][0]['message'] ?? 'خطای نا�
59 شخص';
60 $field = $responseData['messages'][0]['fields'][0] ?? '';
61 } else {
62 $message = 'درخواست با تای�
63 اوت �
64 واجه شد.';
65 $field = '';
66 }
67
68 $errorMessage = 'فرایند اضافه کردن �
69 حصول نا�
70 وفق بود: ' . esc_html($message);
71 if ($field) $errorMessage .= ' (فیلد: ' . esc_html($field) . ')';
72
73 throw new \Exception(esc_html($errorMessage));
74 }
75
76 if (is_wp_error($request)) {
77 $body = $request['body'] ?? '';
78
79 if (is_string($body))
80 $responseData = json_decode($body, true);
81 else $responseData = $body;
82
83 $message = $responseData[0]['message'] ?? 'خطای نا�
84 شخص';
85 throw new \Exception('درخواست �
86 وفقیت آ�
87 یز نبود: ' . esc_html($message));
88 }
89
90 $responseData = json_decode($request['body'], true);
91 if (isset($responseData['id'])) {
92 if (isset($responseData['variants'])) {
93 $product = wc_get_product($productId);
94
95 if ($product && $product->is_type('variable')) {
96 $wcVariations = [];
97 $attributes = $product->get_attributes();
98
99 foreach ($product->get_children() as $variationId) {
100 $variation = wc_get_product($variationId);
101 $attributeValues = [];
102
103 foreach ($attributes as $attributeName => $attribute) {
104 if ($attribute->get_variation()) {
105 $cleanAttributeName = str_replace('attribute_', '', $attributeName);
106 $value = $variation->get_attribute($cleanAttributeName);
107
108 $value = urldecode($value);
109 $value = trim($value);
110 $value = mb_strtolower($value, 'UTF-8');
111 $value = str_replace(['ي', 'ك'], ['ی', 'ک'], $value);
112 $value = str_replace(['-', '_', '', ''], ' ', $value);
113 $value = preg_replace('/\s+/', ' ', $value);
114
115 if (!empty($value)) {
116 $attributeValues[] = $value;
117 }
118 }
119 }
120
121 if (!empty($attributeValues)) {
122 $key = implode("_", $attributeValues);
123 $wcVariations[$key] = $variationId;
124 }
125 }
126
127 $syncBasalamVariations = [];
128 foreach ($responseData['variants'] as $variant) {
129 $attributeValues = [];
130 if (!empty($variant['properties'])) {
131 foreach ($variant['properties'] as $property) {
132 $val = trim($property['value']['title']);
133 $val = mb_strtolower($val, 'UTF-8');
134 $val = str_replace(['ي', 'ك'], ['ی', 'ک'], $val);
135 $val = str_replace(['-', '_', '', ''], ' ', $val);
136 $val = preg_replace('/\s+/', ' ', $val);
137 if (!empty($val)) {
138 $attributeValues[] = $val;
139 }
140 }
141 }
142
143 if (!empty($attributeValues)) {
144 $key = implode("_", $attributeValues);
145 $syncBasalamVariations[$key] = $variant['id'];
146 }
147 }
148
149 $variationMapping = [];
150 foreach ($wcVariations as $key => $wcVarId) {
151 if (isset($syncBasalamVariations[$key])) {
152 $variationMapping[$wcVarId] = $syncBasalamVariations[$key];
153 update_post_meta($wcVarId, 'sync_basalam_variation_id', $syncBasalamVariations[$key]);
154 }
155 }
156 }
157 }
158
159 update_post_meta($productId, ProductMetaKey::basalamProductId(), $responseData['id']);
160 update_post_meta($productId, ProductMetaKey::basalamProductStatus(), 2976);
161 update_post_meta($productId, ProductMetaKey::basalamProductSyncStatus(), 'synced');
162
163 $result = [
164 'success' => true,
165 'message' => '�
166 حصول با �
167 وفقیت به باسلا�
168 اضافه شد.',
169 'status_code' => 200,
170 'basalam_id' => $responseData['id'],
171 ];
172
173 do_action('sync_basalam_after_create_product_api', $productId, $responseData, $result);
174
175 return $result;
176 }
177
178 throw new \Exception("فرایند اضافه کردن �
179 حصول نا�
180 وفق بود");
181 }
182
183 private function ValidCreateProductData(array $productData, int $productId): void
184 {
185 $validation = $this->validator->validate($productData, $productId);
186
187 if (!$validation['valid']) {
188 throw NonRetryableException::invalidData(esc_html($validation['message']));
189 }
190 }
191 }
192