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
← All changes | includes/Services/Products/FetchCommission.php +66 -3 1.10.151.10.21 View file →
@@ -7,11 +7,61 @@
7 7
8 8 defined('ABSPATH') || exit;
9 9 class FetchCommission
10 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 +
11 62 public static function fetchCategoryCommission($categoryIds)
12 63 {
13 - $apiservice = syncBasalamContainer()->get(ApiServiceManager::class);
14 64 $queryParams = [];
15 65
16 66 if (isset($categoryIds[0]) && is_numeric($categoryIds[0])) {
17 67 $queryParams[] = "product.category.level1=" . intval($categoryIds[0]);
@@ -24,17 +74,30 @@
24 74 }
25 75
26 76 if (empty($queryParams)) return false;
27 77
78 + $apiservice = syncBasalamContainer()->get(ApiServiceManager::class);
28 79 $url = Endpoints::COMMISSION . '?' . implode("&", $queryParams);
29 80
30 81 try {
31 82 $result = $apiservice->get($url);
32 - } catch (\Exception $e) {
83 + } catch (\Throwable $e) {
33 84 return 0;
34 85 }
35 86
36 - $decodedBody = json_decode($result['body'], true);
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 + }
37 100
38 101 $commissionPercent = $decodedBody['commission_data']['commission_percent'];
39 102
40 103 if ($commissionPercent) return $commissionPercent;