PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / 1.10.19
ووسلام – همگام سازی ووکامرس و باسلام v1.10.19
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.10.19, at includes/Admin/Product/Data/Services/PriceService.php

151 lines 5.2 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\PriceChangeField;
6 use SyncBasalam\Admin\Settings\SettingsConfig;
7 use SyncBasalam\Logger\Logger;
8 use SyncBasalam\Services\Products\FetchCommission;
9 use SyncBasalam\Utilities\PriceAdjustment;
10
11 defined('ABSPATH') || exit;
12
13 class PriceService
14 {
15 public function calculateFinalPrice($product): ?int
16 {
17 $price = $this->getBasePrice($product);
18 if (!$price) return null;
19
20 $categoryIds = $this->getCategoryIds($product);
21 return $this->applyPriceCalculations($price, $categoryIds, $product);
22 }
23
24 private function getBasePrice($product)
25 {
26 $priceField = syncBasalamSettings()->getSettings(SettingsConfig::PRODUCT_PRICE_FIELD);
27 $regularPrice = $product->get_regular_price();
28 $salePrice = $product->get_sale_price();
29
30 if ($priceField === 'original_price' || $priceField === 'sale_strikethrough_price') {
31 return $regularPrice && is_numeric($regularPrice) ? floatval($regularPrice) : null;
32 }
33
34 if ($priceField === 'sale_price') {
35 if ($salePrice && is_numeric($salePrice)) return floatval($salePrice);
36 return $regularPrice && is_numeric($regularPrice) ? floatval($regularPrice) : null;
37 }
38
39 return null;
40 }
41
42 private function applyPriceCalculations(float $price, array $categoryIds, $product): ?int
43 {
44 $priceChangeValue = $this->getPriceChangeValue($product);
45 $roundMode = syncBasalamSettings()->getSettings(SettingsConfig::ROUND_PRICE);
46 $currency = get_woocommerce_currency();
47
48 $finalPrice = $this->convertToRial($price, $currency);
49 if (!$finalPrice) return null;
50
51
52 if ($priceChangeValue !== '' && $priceChangeValue !== '0') $finalPrice = $this->applyPriceChange($finalPrice, $priceChangeValue, $categoryIds);
53
54 if ($roundMode && $roundMode != 'none') $finalPrice = $this->applyRounding($finalPrice, $roundMode);
55
56 if ($finalPrice < 1000) return null;
57
58 return intval($finalPrice);
59 }
60
61 private function getPriceChangeValue($product): string
62 {
63 $productId = method_exists($product, 'get_id') ? intval($product->get_id()) : 0;
64
65 if ($productId > 0) {
66 $productValue = PriceAdjustment::normalize($this->getProductPriceChangeMeta($productId));
67
68 if ($productValue !== null) return $productValue;
69
70 if (method_exists($product, 'get_parent_id')) {
71 $parentId = intval($product->get_parent_id());
72 $parentValue = $parentId > 0
73 ? PriceAdjustment::normalize($this->getProductPriceChangeMeta($parentId))
74 : null;
75
76 if ($parentValue !== null) return $parentValue;
77 }
78 }
79
80 return (string) (PriceAdjustment::normalize(syncBasalamSettings()->getSettings(SettingsConfig::PRICE_CHANGE_VALUE)) ?? '0');
81 }
82
83 private function getProductPriceChangeMeta(int $productId): string
84 {
85 return (string) get_post_meta($productId, PriceChangeField::META_KEY, true);
86 }
87
88 private function convertToRial(float $price, string $currency)
89 {
90 if ($currency === 'IRT') return $price * 10;
91 if ($currency === 'IRHT') return $price * 10000;
92 if ($currency === 'IRHR') return $price * 1000;
93
94 return $price;
95 }
96
97 private function applyPriceChange(float $price, string $priceChangeValue, array $categoryIds): float
98 {
99 if (PriceAdjustment::isCommission($priceChangeValue)) return $this->applyCommissionCalculation($price, $categoryIds);
100
101 $value = intval($priceChangeValue);
102
103 if (PriceAdjustment::isPercent($value)) return $price + ($price * ($value / 100));
104
105 return $price + ($value * 10);
106 }
107
108 private function applyCommissionCalculation(float $price, array $categoryIds): float
109 {
110 $categoryPercent = floatval(FetchCommission::fetchCategoryCommission($categoryIds));
111
112 if ($categoryPercent <= 0 || $categoryPercent >= 100) return $price;
113
114 $multiplier = 1 / (1 - ($categoryPercent / 100));
115 $maxMultiplier = 1 + (PriceAdjustment::MAX_PERCENT / 100);
116
117 if ($multiplier > $maxMultiplier) {
118 Logger::warning('کار�
119 زد دسته‌بندی باسلا�
120 خارج از بازه �
121 نطقی بود و افزایش قی�
122 ت به سقف �
123 جاز �
124 حدود شد.', [
125 'category_ids' => $categoryIds,
126 'commission_percent' => $categoryPercent,
127 'max_percent' => PriceAdjustment::MAX_PERCENT,
128 ]);
129
130 $multiplier = $maxMultiplier;
131 }
132
133 return $price * $multiplier;
134 }
135
136 private function applyRounding(float $price, $mode): float
137 {
138 if ($mode === 'up') return ceil($price / 10000) * 10000;
139
140 if ($mode === 'down') return floor($price / 10000) * 10000;
141
142 return $price;
143 }
144
145 private function getCategoryIds($product): array
146 {
147 $categoryService = new CategoryService();
148 return $categoryService->getCategoryIds($product);
149 }
150 }
151