PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / 1.10.19
ووسلام – همگام سازی ووکامرس و باسلام v1.10.19
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 / Admin / Product / Services / ProductSyncService.php

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

140 lines 4.5 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 $payload['cursor'] = $cursor;
113 $data = $this->jobManager->createJob(
114 self::JOB_TYPE_AUTO_CONNECT,
115 'pending',
116 json_encode($payload)
117 );
118 }
119
120 private function isValidProductForCreate(int $productId): bool
121 {
122 $product = wc_get_product($productId);
123 return $product && $product->get_status() === 'publish';
124 }
125
126 private function filterValidProductsForUpdate(array $productIds): array
127 {
128 $validIds = [];
129
130 foreach ($productIds as $productId) {
131 $basalamProductId = get_post_meta($productId, ProductMetaKey::basalamProductId(), true);
132 if (!empty($basalamProductId)) {
133 $validIds[] = $productId;
134 }
135 }
136
137 return $validIds;
138 }
139 }
140