PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / 1.10.21
ووسلام – همگام سازی ووکامرس و باسلام v1.10.21
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 1.8.8 1.8.5 All 54 releases
sync-basalam / includes / Services / Products / FetchCommission.php

FetchCommission.php in ووسلام – همگام سازی ووکامرس و باسلام 1.10.21, at includes/Services/Products/FetchCommission.php

107 lines 3.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\Services\Products;
4
5 use SyncBasalam\Config\Endpoints;
6 use SyncBasalam\Services\ApiServiceManager;
7
8 defined('ABSPATH') || exit;
9 class FetchCommission
10 {
11 private static array $productCommissionCache = [];
12
13 /**
14 * Fetch the effective commission for an existing Basalam product.
15 *
16 * The product endpoint returns both the generic and category commission
17 * sections. For an update, Basalam's effective value is the value in
18 * commission_data.commission_percent.
19 */
20 public static function fetchProductCommission($basalamProductId)
21 {
22 if (!is_numeric($basalamProductId) || intval($basalamProductId) <= 0) return 0;
23
24 $basalamProductId = intval($basalamProductId);
25 if (array_key_exists($basalamProductId, self::$productCommissionCache)) {
26 return self::$productCommissionCache[$basalamProductId];
27 }
28
29 $apiservice = syncBasalamContainer()->get(ApiServiceManager::class);
30 $url = sprintf(Endpoints::PRODUCT_COMMISSION, $basalamProductId);
31
32 try {
33 $result = $apiservice->get($url);
34 } catch (\Throwable $e) {
35 return 0;
36 }
37
38 if (!is_array($result) || !array_key_exists('body', $result)) {
39 return 0;
40 }
41
42 $body = $result['body'];
43 if (is_string($body)) $body = json_decode($body, true);
44
45 if (!is_array($body)
46 || !isset($body['commission_data'])
47 || !is_array($body['commission_data'])
48 || !array_key_exists('commission_percent', $body['commission_data'])
49 || !is_numeric($body['commission_data']['commission_percent'])
50 ) {
51 return 0;
52 }
53
54 return self::$productCommissionCache[$basalamProductId] = $body['commission_data']['commission_percent'];
55 }
56
57 public static function resetProductCommissionCache(): void
58 {
59 self::$productCommissionCache = [];
60 }
61
62 public static function fetchCategoryCommission($categoryIds)
63 {
64 $queryParams = [];
65
66 if (isset($categoryIds[0]) && is_numeric($categoryIds[0])) {
67 $queryParams[] = "product.category.level1=" . intval($categoryIds[0]);
68 }
69 if (isset($categoryIds[1]) && is_numeric($categoryIds[1])) {
70 $queryParams[] = "product.category.level2=" . intval($categoryIds[1]);
71 }
72 if (isset($categoryIds[2]) && is_numeric($categoryIds[2])) {
73 $queryParams[] = "product.category.level3=" . intval($categoryIds[2]);
74 }
75
76 if (empty($queryParams)) return false;
77
78 $apiservice = syncBasalamContainer()->get(ApiServiceManager::class);
79 $url = Endpoints::COMMISSION . '?' . implode("&", $queryParams);
80
81 try {
82 $result = $apiservice->get($url);
83 } catch (\Throwable $e) {
84 return 0;
85 }
86
87 if (!is_array($result) || !array_key_exists('body', $result)) return 0;
88
89 $decodedBody = $result['body'];
90 if (is_string($decodedBody)) $decodedBody = json_decode($decodedBody, true);
91
92 if (!is_array($decodedBody)
93 || !isset($decodedBody['commission_data'])
94 || !is_array($decodedBody['commission_data'])
95 || !array_key_exists('commission_percent', $decodedBody['commission_data'])
96 || !is_numeric($decodedBody['commission_data']['commission_percent'])
97 ) {
98 return 0;
99 }
100
101 $commissionPercent = $decodedBody['commission_data']['commission_percent'];
102
103 if ($commissionPercent) return $commissionPercent;
104 return 0;
105 }
106 }
107