PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / 1.8.5
ووسلام – همگام سازی ووکامرس و باسلام v1.8.5
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 / AutoConnectProducts.php

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

151 lines 5.0 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\Logger\Logger;
6 use SyncBasalam\Jobs\Exceptions\RetryableException;
7 use SyncBasalam\Jobs\Exceptions\NonRetryableException;
8 use SyncBasalam\Utilities\ProductMetaKey;
9
10 defined('ABSPATH') || exit;
11
12 class AutoConnectProducts
13 {
14 public function checkSameProduct($title = null, $cursor = null)
15 {
16 try {
17 $getProductData = new FetchProductsData();
18 if ($title) {
19 $title = mb_substr($title, 0, 120);
20 $syncBasalamProducts = $getProductData->getProductData($title);
21 } else {
22 $syncBasalamProducts = $getProductData->getProductData(null, $cursor);
23 }
24
25 if (!is_array($syncBasalamProducts) || !isset($syncBasalamProducts['data'])) {
26 return $title ? [] : [
27 'error' => true,
28 'message' => 'خطا در دریافت اطلاعات �
29 حصولات',
30 'status_code' => 400,
31 'has_more' => false,
32 'next_cursor' => null,
33 ];
34 }
35
36 if ($title) {
37 return $syncBasalamProducts['data'];
38 }
39
40 global $wpdb;
41
42 $matchedProducts = [];
43 $productIdMetaKey = ProductMetaKey::basalamProductId();
44
45 foreach ($syncBasalamProducts['data'] as $syncBasalamProduct) {
46 $normalizedTitle = trim($syncBasalamProduct['title']);
47
48 if (mb_strlen($normalizedTitle) >= 120) {
49 $likeTitle = $normalizedTitle . '%';
50 } else {
51 $likeTitle = $normalizedTitle;
52 }
53
54 $productId = $wpdb->get_var(
55 $wpdb->prepare("
56 SELECT p.ID
57 FROM {$wpdb->posts} p
58 LEFT JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id AND pm.meta_key = %s
59 WHERE p.post_type = 'product'
60 AND p.post_status = 'publish'
61 AND pm.post_id IS NULL
62 AND LOWER(p.post_title) LIKE LOWER(%s)
63 LIMIT 1
64 ", $productIdMetaKey, $likeTitle)
65 );
66
67 if ($productId) {
68 $connectProductService = new ConnectSingleProductService();
69 $result = $connectProductService->connectProductById($productId, $syncBasalamProduct['id']);
70
71 if ($result) {
72 Logger::info($syncBasalamProduct['title'] . ' به �
73 حصول �
74 شابه خود در باسلا�
75
76 تصل شد', [
77 'product_id' => $productId,
78 'ع�
79 لیات' => "اتصال اتو�
80 اتیک �
81 حصولات ووکا�
82 رس و باسلا�
83 ",
84 ]);
85 }
86
87 $matchedProducts[] = $syncBasalamProduct;
88 }
89 }
90
91 $hasMore = !empty($syncBasalamProducts['has_more']);
92 $nextCursor = $syncBasalamProducts['next_cursor'] ?? null;
93
94 if ($hasMore && !empty($nextCursor)) {
95 return [
96 'success' => true,
97 'message' => '�
98 حصولات با �
99 وفقیت به صف اتصال افزوده شدند.',
100 'status_code' => 200,
101 'has_more' => true,
102 'next_cursor' => $nextCursor,
103 ];
104 } else {
105 if (!empty($matchedProducts)) {
106 return [
107 'success' => true,
108 'message' => 'اتصال �
109 حصولات کا�
110 ل شد.',
111 'status_code' => 200,
112 'has_more' => false,
113 'next_cursor' => null,
114 ];
115 } else {
116 return [
117 'error' => true,
118 'message' => '�
119 حصول �
120 شابهی یافت نشد.',
121 'status_code' => 404,
122 'has_more' => false,
123 'next_cursor' => null,
124 ];
125 }
126 }
127 } catch (RetryableException $e) {
128 Logger::error("خطا در اتصال خودکار �
129 حصولات: " . $e->getMessage(), [
130 'operation' => 'اتصال خودکار �
131 حصولات',
132 ]);
133 throw $e;
134 } catch (NonRetryableException $e) {
135 Logger::error("خطا در اتصال خودکار �
136 حصولات: " . $e->getMessage(), [
137 'operation' => 'اتصال خودکار �
138 حصولات',
139 ]);
140 throw $e;
141 } catch (\Exception $e) {
142 Logger::error("خطا در اتصال خودکار �
143 حصولات: " . $e->getMessage(), [
144 'operation' => 'اتصال خودکار �
145 حصولات',
146 ]);
147 throw $e;
148 }
149 }
150 }
151