PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / 1.10.9
ووسلام – همگام سازی ووکامرس و باسلام v1.10.9
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.9, at includes/Admin/Product/Services/ProductSyncService.php

126 lines 3.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
9 defined('ABSPATH') || exit;
10
11 class ProductSyncService
12 {
13 private const JOB_TYPE_CREATE_ALL = 'sync_basalam_create_all_products';
14 private const JOB_TYPE_CREATE_SINGLE = 'sync_basalam_create_single_product';
15 private const JOB_TYPE_UPDATE_SINGLE = 'sync_basalam_update_single_product';
16 private const JOB_TYPE_AUTO_CONNECT = 'sync_basalam_auto_connect_products';
17
18 private $jobManager;
19
20 public function __construct($jobManager = null)
21 {
22 $this->jobManager = $jobManager ?: syncBasalamContainer()->get(JobManager::class);
23 }
24
25 public function enqueueBulkCreate(bool $includeOutOfStock = false, int $postsPerPage = 100): array
26 {
27 $existingJob = $this->jobManager->getJob([
28 'job_type' => self::JOB_TYPE_CREATE_ALL,
29 'status' => 'pending',
30 ]);
31
32 if ($existingJob) {
33 return [
34 'success' => false,
35 'message' => 'در حال حاضر یک ع�
36 لیات در صف انتظار است.',
37 'status_code' => 409,
38 ];
39 }
40
41 $initialData = json_encode([
42 'posts_per_page' => $postsPerPage,
43 'include_out_of_stock' => $includeOutOfStock,
44 ]);
45
46 $this->jobManager->createJob(
47 self::JOB_TYPE_CREATE_ALL,
48 'pending',
49 $initialData
50 );
51
52 return [
53 'success' => true,
54 'message' => '�
55 حصولات با �
56 وفقیت به صف ایجاد افزوده شدند.',
57 'status_code' => 200,
58 ];
59 }
60
61 public function enqueueSelectedForCreate(array $productIds): void
62 {
63 foreach ($productIds as $productId) {
64 if (!$this->isValidProductForCreate($productId)) {
65 continue;
66 }
67
68 $basalamProductId = get_post_meta($productId, ProductMetaKey::basalamProductId(), true);
69 if (empty($basalamProductId)) {
70 $this->jobManager->createJob(
71 self::JOB_TYPE_CREATE_SINGLE,
72 'pending',
73 json_encode(['product_id' => $productId])
74 );
75 }
76 }
77 }
78
79 public function enqueueSelectedForUpdate(array $productIds): void
80 {
81 if (!SettingsManager::isProductUpdateSelectionValid()) return;
82
83 $validProductIds = $this->filterValidProductsForUpdate($productIds);
84
85 foreach ($validProductIds as $productId) {
86 if (!$this->jobManager->hasProductJobInProgress($productId, self::JOB_TYPE_UPDATE_SINGLE)) {
87 $this->jobManager->createJob(
88 self::JOB_TYPE_UPDATE_SINGLE,
89 'pending',
90 json_encode(['product_id' => $productId])
91 );
92 }
93 }
94 }
95
96 public function enqueueAutoConnect($cursor = null): void
97 {
98 $payload['cursor'] = $cursor;
99 $data = $this->jobManager->createJob(
100 self::JOB_TYPE_AUTO_CONNECT,
101 'pending',
102 json_encode($payload)
103 );
104 }
105
106 private function isValidProductForCreate(int $productId): bool
107 {
108 $product = wc_get_product($productId);
109 return $product && $product->get_status() === 'publish';
110 }
111
112 private function filterValidProductsForUpdate(array $productIds): array
113 {
114 $validIds = [];
115
116 foreach ($productIds as $productId) {
117 $basalamProductId = get_post_meta($productId, ProductMetaKey::basalamProductId(), true);
118 if (!empty($basalamProductId)) {
119 $validIds[] = $productId;
120 }
121 }
122
123 return $validIds;
124 }
125 }
126