PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / 1.9.0
ووسلام – همگام سازی ووکامرس و باسلام v1.9.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 / Operations / ConnectProduct.php

ConnectProduct.php in ووسلام – همگام سازی ووکامرس و باسلام 1.9.0, at includes/Admin/Product/Operations/ConnectProduct.php

117 lines 3.6 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\Operations;
4
5 use SyncBasalam\Services\Products\ConnectSingleProductService;
6 use SyncBasalam\Services\Products\AutoConnectProducts;
7 use SyncBasalam\JobManager;
8 use SyncBasalam\Admin\Components\SingleProductPageComponents;
9
10 defined('ABSPATH') || exit;
11
12 class ConnectProduct
13 {
14 public function handleConnectProduct(): array
15 {
16 if (!current_user_can('manage_woocommerce')) {
17 return [
18 'success' => false,
19 'message' => 'تنها �
20 دیر کل ا�
21 کان تغییر وضعیت سفارش را دارد.',
22 'status_code' => 400,
23 ];
24 }
25
26 $woo_product_id = isset($_POST['woo_product_id']) ? intval($_POST['woo_product_id']) : '';
27 $sync_basalam_product_id = isset($_POST['basalam_product_id']) ? sanitize_text_field(wp_unslash($_POST['basalam_product_id'])) : '';
28
29 if (!$woo_product_id || !$sync_basalam_product_id) {
30 return [
31 'success' => false,
32 'message' => 'داده های ورودی ناقص است.',
33 'status_code' => 400,
34 ];
35 }
36
37 $connect_status = ConnectSingleProductService::connectProductById($woo_product_id, $sync_basalam_product_id);
38
39 $job_manager = syncBasalamContainer()->get(JobManager::class);
40 $job_manager->createJob(
41 'sync_basalam_update_single_product',
42 'pending',
43 $woo_product_id,
44 );
45
46 if ($connect_status) {
47 return [
48 'success' => true,
49 'message' => 'اتصال �
50 حصولات با �
51 وفقیت انجا�
52 شد.',
53 'status_code' => 200,
54 ];
55 } else {
56 return [
57 'success' => false,
58 'message' => 'این �
59 حصول به �
60 حصول دیگری �
61 تصل شده است.',
62 'status_code' => 400,
63 ];
64 }
65 }
66
67 public function handleSearchProducts(): void
68 {
69 if (!current_user_can('edit_posts')) {
70 wp_send_json_error("دسترسی غیر�
71 جاز.");
72 }
73
74 $title = isset($_POST['title']) ? sanitize_text_field(wp_unslash($_POST['title'])) : '';
75
76 if (empty($title)) {
77 echo '<p>عنوان �
78 حصول وارد نشده است.</p>';
79 wp_die();
80 }
81
82 $productId = isset($_POST['woo_product_id']) ? intval($_POST['woo_product_id']) : 0;
83 $this->renderProductsByTitle($title, $productId);
84 wp_die();
85 }
86
87 public function renderProductsByTitle(string $title, int $productId): void
88 {
89 $products = $this->getProductsByTitle($title);
90 $this->renderProductCards($products, $productId);
91 }
92
93 private function getProductsByTitle(string $title): array
94 {
95 $title = trim($title);
96 if ($title === '') return [];
97
98 $checker = new AutoConnectProducts();
99 $result = $checker->checkSameProduct($title);
100
101 return is_array($result) && !isset($result['error']) ? $result : [];
102 }
103
104 private function renderProductCards(array $products, int $productId): void
105 {
106 if (!empty($products)) {
107 foreach ($products as $product) {
108 if (!is_array($product)) continue;
109 SingleProductPageComponents::renderProductCard($product, $productId);
110 }
111 } else {
112 echo '<p class="basalam--no-match">�
113 حصولی با این عنوان پیدا نشد.</p>';
114 }
115 }
116 }
117