PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / 1.9.1
ووسلام – همگام سازی ووکامرس و باسلام v1.9.1
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 / Data / Services / PriceService.php

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

128 lines 4.4 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\Data\Services;
4
5 use SyncBasalam\Admin\Product\elements\SingleProduct\PriceIncreaseField;
6 use SyncBasalam\Admin\Settings\SettingsConfig;
7 use SyncBasalam\Services\Products\FetchCommission;
8
9 defined('ABSPATH') || exit;
10
11 class PriceService
12 {
13 public function calculateFinalPrice($product): ?int
14 {
15 $price = $this->getBasePrice($product);
16 if (!$price) return null;
17
18 $categoryIds = $this->getCategoryIds($product);
19 return $this->applyPriceCalculations($price, $categoryIds, $product);
20 }
21
22 private function getBasePrice($product)
23 {
24 $priceField = syncBasalamSettings()->getSettings(SettingsConfig::PRODUCT_PRICE_FIELD);
25 $regularPrice = $product->get_regular_price();
26 $salePrice = $product->get_sale_price();
27
28 if ($priceField === 'original_price' || $priceField === 'sale_strikethrough_price') {
29 return $regularPrice && is_numeric($regularPrice) ? floatval($regularPrice) : null;
30 }
31
32 if ($priceField === 'sale_price') {
33 if ($salePrice && is_numeric($salePrice)) return floatval($salePrice);
34 return $regularPrice && is_numeric($regularPrice) ? floatval($regularPrice) : null;
35 }
36
37 return null;
38 }
39
40 private function applyPriceCalculations(float $price, array $categoryIds, $product): ?int
41 {
42 $increaseValue = $this->getIncreaseValue($product);
43 $roundMode = syncBasalamSettings()->getSettings(SettingsConfig::ROUND_PRICE);
44 $currency = get_woocommerce_currency();
45
46 $finalPrice = $this->convertToRial($price, $currency);
47 if (!$finalPrice) return null;
48
49 if ($increaseValue) $finalPrice = $this->applyIncrease($finalPrice, $increaseValue, $categoryIds);
50 if ($roundMode && $roundMode != 'none') $finalPrice = $this->applyRounding($finalPrice, $roundMode);
51
52 if ($finalPrice < 1000) return null;
53
54 return intval($finalPrice);
55 }
56
57 private function getIncreaseValue($product): int
58 {
59 $productId = method_exists($product, 'get_id') ? intval($product->get_id()) : 0;
60
61 if ($productId > 0) {
62 $productIncrease = $this->getProductIncreaseMeta($productId);
63
64 if ($productIncrease !== '' && is_numeric($productIncrease)) {
65 return intval($productIncrease);
66 }
67
68 if (method_exists($product, 'get_parent_id')) {
69 $parentId = intval($product->get_parent_id());
70 $parentIncrease = $parentId > 0 ? $this->getProductIncreaseMeta($parentId) : '';
71
72 if ($parentIncrease !== '' && is_numeric($parentIncrease)) {
73 return intval($parentIncrease);
74 }
75 }
76 }
77
78 return intval(syncBasalamSettings()->getSettings(SettingsConfig::INCREASE_PRICE_VALUE));
79 }
80
81 private function getProductIncreaseMeta(int $productId): string
82 {
83 return (string) get_post_meta($productId, PriceIncreaseField::META_KEY, true);
84 }
85
86 private function convertToRial(float $price, string $currency)
87 {
88 if ($currency === 'IRT') return $price * 10;
89 if ($currency === 'IRHT') return $price * 10000;
90 if ($currency === 'IRHR') return $price * 1000;
91
92 return $price;
93 }
94
95 private function applyIncrease(float $price, int $increaseValue, array $categoryIds): float
96 {
97 if ($increaseValue === -1) return $this->applyCommissionCalculation($price, $categoryIds);
98
99 if ($increaseValue <= 100) return $price + ($price * ($increaseValue / 100));
100
101 return $price + ($increaseValue * 10);
102 }
103
104 private function applyCommissionCalculation(float $price, array $categoryIds): float
105 {
106 $categoryPercent = FetchCommission::fetchCategoryCommission($categoryIds);
107
108 if ($categoryPercent > 0 && $categoryPercent < 100) return $price / (1 - ($categoryPercent / 100));
109 else return $price;
110
111 }
112
113 private function applyRounding(float $price, $mode): float
114 {
115 if ($mode === 'up') return ceil($price / 10000) * 10000;
116
117 if ($mode === 'down') return floor($price / 10000) * 10000;
118
119 return $price;
120 }
121
122 private function getCategoryIds($product): array
123 {
124 $categoryService = new CategoryService();
125 return $categoryService->getCategoryIds($product);
126 }
127 }
128