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 / Admin / Product / Services / ProductSyncService.php

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

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