PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / 1.10.23
ووسلام – همگام سازی ووکامرس و باسلام v1.10.23
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 1.10.1 1.10.0 1.9.2 1.9.1 1.9.0 All 56 releases
sync-basalam / includes / Admin / Product / Services / ProductSyncService.php

ProductSyncService.php in ووسلام – همگام سازی ووکامرس و باسلام 1.10.23, at includes/Admin/Product/Services/ProductSyncService.php

154 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SyncBasalam\Admin\Product\Services;
4
5 use SyncBasalam\JobManager;
6 use SyncBasalam\Admin\Settings\SettingsManager;
7 use SyncBasalam\Utilities\ProductMetaKey;
8 use SyncBasalam\Services\VendorSyncPolicy;
9
10 defined('ABSPATH') || exit;
11
12 class ProductSyncService
13 {
14 private const JOB_TYPE_CREATE_ALL = 'sync_basalam_create_all_products';
15 private const JOB_TYPE_CREATE_SINGLE = 'sync_basalam_create_single_product';
16 private const JOB_TYPE_UPDATE_SINGLE = 'sync_basalam_update_single_product';
17 private const JOB_TYPE_AUTO_CONNECT = 'sync_basalam_auto_connect_products';
18
19 private $jobManager;
20
21 public function __construct($jobManager = null)
22 {
23 $this->jobManager = $jobManager ?: syncBasalamContainer()->get(JobManager::class);
24 }
25
26 public function enqueueBulkCreate(bool $includeOutOfStock = false, int $postsPerPage = 100): array
27 {
28 $vendorSyncPolicy = syncBasalamContainer()->get(VendorSyncPolicy::class);
29 if (!$vendorSyncPolicy->canCreate()) {
30 return [
31 'success' => false,
32 'message' => $vendorSyncPolicy->getRestrictionMessage(false),
33 'status_code' => 409,
34 ];
35 }
36
37 $existingJob = $this->jobManager->getJob([
38 'job_type' => self::JOB_TYPE_CREATE_ALL,
39 'status' => 'pending',
40 ]);
41
42 if ($existingJob) {
43 return [
44 'success' => false,
45 'message' => 'در حال حاضر یک ع�
46 لیات در صف انتظار است.',
47 'status_code' => 409,
48 ];
49 }
50
51 $initialData = json_encode([
52 'posts_per_page' => $postsPerPage,
53 'include_out_of_stock' => $includeOutOfStock,
54 ]);
55
56 $this->jobManager->createJob(
57 self::JOB_TYPE_CREATE_ALL,
58 'pending',
59 $initialData
60 );
61
62 return [
63 'success' => true,
64 'message' => '�
65 حصولات با �
66 وفقیت به صف ایجاد افزوده شدند.',
67 'status_code' => 200,
68 ];
69 }
70
71 public function enqueueSelectedForCreate(array $productIds): void
72 {
73 if (!syncBasalamContainer()->get(VendorSyncPolicy::class)->canCreate()) return;
74
75 foreach ($productIds as $productId) {
76 if (!$this->isValidProductForCreate($productId)) {
77 continue;
78 }
79
80 $basalamProductId = get_post_meta($productId, ProductMetaKey::basalamProductId(), true);
81 if (empty($basalamProductId)) {
82 $this->jobManager->createJob(
83 self::JOB_TYPE_CREATE_SINGLE,
84 'pending',
85 json_encode(['product_id' => $productId])
86 );
87 }
88 }
89 }
90
91 public function enqueueSelectedForUpdate(array $productIds): void
92 {
93 $vendorSyncPolicy = syncBasalamContainer()->get(VendorSyncPolicy::class);
94 if (!$vendorSyncPolicy->canUpdate()) return;
95 if (!$vendorSyncPolicy->shouldRestrictUpdateFields(false) && !SettingsManager::isProductUpdateSelectionValid()) return;
96
97 $validProductIds = $this->filterValidProductsForUpdate($productIds);
98
99 foreach ($validProductIds as $productId) {
100 if (!$this->jobManager->hasProductJobInProgress($productId, self::JOB_TYPE_UPDATE_SINGLE)) {
101 $this->jobManager->createJob(
102 self::JOB_TYPE_UPDATE_SINGLE,
103 'pending',
104 json_encode(['product_id' => $productId])
105 );
106 }
107 }
108 }
109
110 public function enqueueAutoConnect($cursor = null): void
111 {
112 $pendingJob = $this->jobManager->getJob([
113 'job_type' => self::JOB_TYPE_AUTO_CONNECT,
114 'status' => 'pending',
115 ]);
116 $processingJob = $this->jobManager->getJob([
117 'job_type' => self::JOB_TYPE_AUTO_CONNECT,
118 'status' => 'processing',
119 ]);
120
121 if ($pendingJob || $processingJob) return;
122
123 $payload = ['mode' => 'vendor'];
124 if ($cursor !== null) {
125 $payload['cursor'] = $cursor;
126 }
127 $data = $this->jobManager->createJob(
128 self::JOB_TYPE_AUTO_CONNECT,
129 'pending',
130 json_encode($payload)
131 );
132 }
133
134 private function isValidProductForCreate(int $productId): bool
135 {
136 $product = wc_get_product($productId);
137 return $product && $product->get_status() === 'publish';
138 }
139
140 private function filterValidProductsForUpdate(array $productIds): array
141 {
142 $validIds = [];
143
144 foreach ($productIds as $productId) {
145 $basalamProductId = get_post_meta($productId, ProductMetaKey::basalamProductId(), true);
146 if (!empty($basalamProductId)) {
147 $validIds[] = $productId;
148 }
149 }
150
151 return $validIds;
152 }
153 }
154