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 / UpdateSingleProductService.php

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

199 lines 7.3 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\Config\Endpoints;
6 use SyncBasalam\Services\ApiServiceManager;
7 use SyncBasalam\Jobs\Exceptions\RetryableException;
8 use SyncBasalam\Jobs\Exceptions\NonRetryableException;
9 use SyncBasalam\Utilities\ProductMetaKey;
10
11 defined('ABSPATH') || exit;
12
13 class UpdateSingleProductService
14 {
15 private $apiservice;
16
17 public function __construct()
18 {
19 $this->apiservice = syncBasalamContainer()->get(ApiServiceManager::class);
20 }
21
22 public function updateProductInBasalam($productData, $productId)
23 {
24 if (!get_post_type($productId) === 'product') throw NonRetryableException::invalidData('نوع post �
25 حصول نیست.');
26
27 $productData = apply_filters('sync_basalam_product_data_before_update', $productData, $productId);
28
29 do_action('sync_basalam_before_update_product_api', $productId, $productData);
30
31 $syncBasalamProductId = get_post_meta($productId, ProductMetaKey::basalamProductId(), true);
32
33 $url = sprintf(Endpoints::PRODUCT_UPDATE, $syncBasalamProductId);
34
35 try {
36 $request = $this->apiservice->patch($url, $productData);
37 } catch (RetryableException $e) {
38 throw $e;
39 } catch (NonRetryableException $e) {
40 throw $e;
41 } catch (\Exception $e) {
42 throw new \Exception(esc_html('خطا در ارتباط با API باسلا�
43 : ' . $e->getMessage()));
44 }
45
46 $body = $request['body'] ?? '';
47
48 if (is_string($body)) $body = json_decode($body, true);
49
50 if ($request['status_code'] != 200) {
51 if ($request['status_code'] == 403) throw NonRetryableException::unauthorized("این �
52 حصول �
53 تعلق به غرفه فعلی نیست.");
54
55 if (!is_array($body)) $body = [];
56
57 if (isset($body['messages'][0]['message'])) $message = $body['messages'][0]['message'];
58 elseif (isset($body[0]['message'])) $message = $body[0]['message'];
59 else $message = '';
60
61 if (isset($body['messages'][0]['fields'][0])) $field = $body['messages'][0]['fields'][0];
62 elseif (isset($body[0]['fields'][0])) $field = $body[0]['fields'][0];
63 else $field = '';
64
65 $errorMessage = $message ?: 'درخواست با خطا �
66 واجه شد.';
67 if ($field) $errorMessage .= ' (فیلد: ' . $field . ')';
68
69 throw NonRetryableException::permanent(esc_html($errorMessage));
70 }
71
72 if (is_wp_error($request)) throw NonRetryableException::permanent('خطایی در ارتباط با سرور رخ داد.');
73
74 $product = \wc_get_product($productId);
75 if ($product && $product->is_type('variable')) {
76 $variations = $product->get_children();
77 if (isset($body['variants'])) {
78 $wcVariations = [];
79 $attributes = $product->get_attributes();
80
81 foreach ($variations as $variationId) {
82 $variation = \wc_get_product($variationId);
83 $attributeValues = [];
84
85 foreach ($attributes as $attributeName => $attribute) {
86 if ($attribute->get_variation()) {
87 $cleanAttributeName = str_replace('attribute_', '', $attributeName);
88 $value = $variation->get_attribute($cleanAttributeName);
89
90 $value = urldecode($value);
91 $value = trim($value);
92 $value = mb_strtolower($value, 'UTF-8');
93 $value = str_replace(['ي', 'ك'], ['ی', 'ک'], $value);
94 $value = str_replace(['-', '_', '–', '—'], ' ', $value);
95 $value = preg_replace('/\s+/', ' ', $value);
96
97 if (!empty($value)) $attributeValues[] = $value;
98 }
99 }
100
101 if (!empty($attributeValues)) {
102 $key = implode("_", $attributeValues);
103 $wcVariations[$key] = $variationId;
104 }
105 }
106
107 $syncBasalamVariations = [];
108 foreach ($body['variants'] as $variant) {
109 $attributeValues = [];
110 if (!empty($variant['properties'])) {
111 foreach ($variant['properties'] as $property) {
112 $val = $property['value']['title'];
113
114 $val = trim($val);
115 $val = mb_strtolower($val, 'UTF-8');
116 $val = str_replace(['ي', 'ك'], ['ی', 'ک'], $val);
117 $val = str_replace(['-', '_', '–', '—'], ' ', $val);
118 $val = preg_replace('/\s+/', ' ', $val);
119
120 if (!empty($val)) $attributeValues[] = $val;
121 }
122 }
123
124 if (!empty($attributeValues)) {
125 $key = implode("_", $attributeValues);
126 $syncBasalamVariations[$key] = $variant['id'];
127 }
128 }
129
130 foreach ($wcVariations as $key => $wcVarId) {
131 if (isset($syncBasalamVariations[$key])) {
132 update_post_meta($wcVarId, 'sync_basalam_variation_id', $syncBasalamVariations[$key]);
133 }
134 }
135 }
136 }
137
138 update_post_meta($productId, ProductMetaKey::basalamProductSyncStatus(), 'synced');
139
140 $result = [
141 'success' => true,
142 'message' => 'فرایند بروزرسانی �
143 حصول با �
144 وفقیت انجا�
145 شد.',
146 'status_code' => 200,
147 ];
148
149 do_action('sync_basalam_after_update_product_api', $productId, $body, $result);
150
151 return $result;
152 }
153
154 public function updateProductStatus($productId, $status)
155 {
156 $syncBasalamProductId = get_post_meta($productId, ProductMetaKey::basalamProductId(), true);
157 $url = sprintf(Endpoints::PRODUCT_UPDATE, $syncBasalamProductId);
158
159 $data = ["status" => $status];
160
161 $data = apply_filters('sync_basalam_product_status_data_before_update', $data, $productId, $status);
162
163 do_action('sync_basalam_before_update_product_status', $productId, $status, $data);
164
165 try {
166 $request = $this->apiservice->patch($url, $data);
167 } catch (RetryableException $e) {
168 throw $e;
169 } catch (NonRetryableException $e) {
170 throw $e;
171 } catch (\Exception $e) {
172 throw NonRetryableException::permanent(esc_html($e->getMessage()));
173 }
174
175 if (!is_wp_error($request)) {
176 update_post_meta($productId, ProductMetaKey::basalamProductSyncStatus(), 'synced');
177 update_post_meta($productId, ProductMetaKey::basalamProductStatus(), $status);
178
179 $result = [
180 'success' => true,
181 'message' => 'وضعیت �
182 حصول با �
183 وفقیت در باسلا�
184 تغییر کرد.',
185 'status_code' => 200,
186 ];
187
188 do_action('sync_basalam_after_update_product_status', $productId, $status, $result);
189
190 return $result;
191 }
192
193 throw NonRetryableException::permanent("تغییر وضعیت �
194 حصول در باسلا�
195 نا�
196 وفق بود.");
197 }
198 }
199