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/Admin/Product/Data/Services/PriceService.php +55 -11 1.10.201.10.21 View file →
@@ -5,8 +5,9 @@
5 5 use SyncBasalam\Admin\Product\elements\SingleProduct\PriceChangeField;
6 6 use SyncBasalam\Admin\Settings\SettingsConfig;
7 7 use SyncBasalam\Logger\Logger;
8 8 use SyncBasalam\Services\Products\FetchCommission;
9 +use SyncBasalam\Utilities\ProductMetaKey;
9 10 use SyncBasalam\Utilities\PriceAdjustment;
10 11
11 12 defined('ABSPATH') || exit;
12 13
@@ -16,10 +17,9 @@
16 17 {
17 18 $price = $this->getBasePrice($product);
18 19 if (!$price) return null;
19 20
20 - $categoryIds = $this->getCategoryIds($product);
21 - return $this->applyPriceCalculations($price, $categoryIds, $product);
21 + return $this->applyPriceCalculations($price, $product);
22 22 }
23 23
24 24 private function getBasePrice($product)
25 25 {
@@ -38,9 +38,9 @@
38 38
39 39 return null;
40 40 }
41 41
42 - private function applyPriceCalculations(float $price, array $categoryIds, $product): ?int
42 + private function applyPriceCalculations(float $price, $product): ?int
43 43 {
44 44 $priceChangeValue = $this->getPriceChangeValue($product);
45 45 $roundMode = syncBasalamSettings()->getSettings(SettingsConfig::ROUND_PRICE);
46 46 $currency = get_woocommerce_currency();
@@ -48,9 +48,9 @@
48 48 $finalPrice = $this->convertToRial($price, $currency);
49 49 if (!$finalPrice) return null;
50 50
51 51
52 - if ($priceChangeValue !== '' && $priceChangeValue !== '0') $finalPrice = $this->applyPriceChange($finalPrice, $priceChangeValue, $categoryIds);
52 + if ($priceChangeValue !== '' && $priceChangeValue !== '0') $finalPrice = $this->applyPriceChange($finalPrice, $priceChangeValue, $product);
53 53
54 54 if ($roundMode && $roundMode != 'none') $finalPrice = $this->applyRounding($finalPrice, $roundMode);
55 55
56 56 if ($finalPrice < 1000) return null;
@@ -93,11 +93,11 @@
93 93
94 94 return $price;
95 95 }
96 96
97 - private function applyPriceChange(float $price, string $priceChangeValue, array $categoryIds): float
97 + private function applyPriceChange(float $price, string $priceChangeValue, $product = null): float
98 98 {
99 - if (PriceAdjustment::isCommission($priceChangeValue)) return $this->applyCommissionCalculation($price, $categoryIds);
99 + if (PriceAdjustment::isCommission($priceChangeValue)) return $this->applyCommissionCalculation($price, $product);
100 100
101 101 $value = intval($priceChangeValue);
102 102
103 103 if (PriceAdjustment::isPercent($value)) return $price + ($price * ($value / 100));
@@ -104,21 +104,31 @@
104 104
105 105 return $price + ($value * 10);
106 106 }
107 107
108 - private function applyCommissionCalculation(float $price, array $categoryIds): float
108 + private function applyCommissionCalculation(float $price, $product = null): float
109 109 {
110 - $categoryPercent = floatval(FetchCommission::fetchCategoryCommission($categoryIds));
110 + $basalamProductId = $this->getBasalamProductId($product);
111 + $categoryIds = [];
111 112
112 - if ($categoryPercent <= 0 || $categoryPercent >= 100) return $price;
113 + if ($basalamProductId !== null) {
114 + $commissionPercent = FetchCommission::fetchProductCommission($basalamProductId);
115 + } else {
116 + $categoryIds = $this->getCategoryIds($product);
117 + $commissionPercent = FetchCommission::fetchCategoryCommission($categoryIds);
118 + }
113 119
114 - $multiplier = 1 / (1 - ($categoryPercent / 100));
120 + $commissionPercent = floatval($commissionPercent);
121 +
122 + if ($commissionPercent <= 0 || $commissionPercent >= 100) return $price;
123 +
124 + $multiplier = 1 / (1 - ($commissionPercent / 100));
115 125 $maxMultiplier = 1 + (PriceAdjustment::MAX_PERCENT / 100);
116 126
117 127 if ($multiplier > $maxMultiplier) {
118 128 Logger::warning('کارمزد دسته‌بندی باسلام خارج از بازه منطقی بود و افزایش قیمت به سقف مجاز محدود شد.', [
119 129 'category_ids' => $categoryIds,
120 - 'commission_percent' => $categoryPercent,
130 + 'commission_percent' => $commissionPercent,
121 131 'max_percent' => PriceAdjustment::MAX_PERCENT,
122 132 ]);
123 133
124 134 $multiplier = $maxMultiplier;
@@ -124,8 +134,42 @@
124 134 $multiplier = $maxMultiplier;
125 135 }
126 136
127 137 return $price * $multiplier;
138 + }
139 +
140 + /**
141 + * Resolve the remote product id used by the product commission endpoint.
142 + * Variations inherit the connection from their parent product.
143 + */
144 + private function getBasalamProductId($product): ?int
145 + {
146 + if (!is_object($product)) return null;
147 +
148 + $postIds = [];
149 +
150 + if (method_exists($product, 'get_parent_id')) {
151 + $parentId = intval($product->get_parent_id());
152 + if ($parentId > 0) {
153 + $postIds[] = $parentId;
154 + }
155 + }
156 +
157 + if (empty($postIds) && method_exists($product, 'get_id')) {
158 + $productId = intval($product->get_id());
159 + if ($productId > 0) $postIds[] = $productId;
160 + }
161 +
162 + $metaKey = ProductMetaKey::basalamProductId();
163 + foreach (array_unique($postIds) as $postId) {
164 + $basalamProductId = get_post_meta($postId, $metaKey, true);
165 +
166 + if (is_numeric($basalamProductId) && intval($basalamProductId) > 0) {
167 + return intval($basalamProductId);
168 + }
169 + }
170 +
171 + return null;
128 172 }
129 173
130 174 private function applyRounding(float $price, $mode): float
131 175 {