-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathPriceData.php
More file actions
363 lines (322 loc) · 14.4 KB
/
Copy pathPriceData.php
File metadata and controls
363 lines (322 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<?php
/**
* Copyright © Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magmodules\Channable\Service\Product;
use Magento\Bundle\Model\Product\Price;
use Magento\Catalog\Helper\Data as CatalogHelper;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\CatalogPrice;
use Magento\CatalogRule\Model\ResourceModel\RuleFactory as RuleFactory;
class PriceData
{
/**
* @var CatalogPrice
*/
private $commonPriceModel;
/**
* @var RuleFactory
*/
private $ruleFactory;
/**
* @var CatalogHelper
*/
private $catalogHelper;
public function __construct(
CatalogPrice $commonPriceModel,
CatalogHelper $catalogHelper,
RuleFactory $ruleFactory
) {
$this->commonPriceModel = $commonPriceModel;
$this->ruleFactory = $ruleFactory;
$this->catalogHelper = $catalogHelper;
}
/**
* @param array $config
* @param Product $product
*
* @return array
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function execute(array $config, Product $product): array
{
switch ($product->getTypeId()) {
case 'configurable':
/**
* Check if config has a final_price (data catalog_product_index_price)
* If final_price === null product is not salable (out of stock)
*/
if ($product->getData('final_price') === null) {
$price = 0;
$finalPrice = 0;
} else {
$price = $product->getData('price');
$finalPrice = $product->getData('final_price');
$specialPrice = $product->getSpecialPrice();
$product['min_price'] = $product['min_price'] >= 0 ? $product['min_price'] : null;
$product['max_price'] = $product['max_price'] >= 0 ? $product['max_price'] : null;
}
break;
case 'grouped':
$groupedPriceType = null;
if (!empty($config['price_config']['grouped_price_type'])) {
$groupedPriceType = $config['price_config']['grouped_price_type'];
}
$groupedPrices = $this->getGroupedPrices($product, $config);
$price = $groupedPrices['min_price'];
$finalPrice = $groupedPrices['min_price'];
$product['min_price'] = $groupedPrices['min_price'];
$product['max_price'] = $groupedPrices['max_price'];
$product['total_price'] = $groupedPrices['total_price'];
if ($groupedPriceType == 'max') {
$price = $groupedPrices['max_price'];
$finalPrice = $price;
}
if ($groupedPriceType == 'total') {
$price = $groupedPrices['total_price'];
$finalPrice = $price;
}
break;
case 'bundle':
$price = $product->getPrice();
if ((int)$product->getPriceType() === Price::PRICE_TYPE_DYNAMIC) {
$product['min_price'] = $product->getPriceInfo()->getPrice('final_price')->getMinimalPrice()->getBaseAmount();
$product['max_price'] = $product->getPriceInfo()->getPrice('final_price')->getMaximalPrice()->getBaseAmount();
}
$finalPrice = $product->getFinalPrice();
$specialPrice = $product->getSpecialPrice();
$rulePrice = $this->ruleFactory->create()->getRulePrice(
$config['timestamp'],
$config['website_id'],
'',
$product->getId()
);
if ($rulePrice !== null && $rulePrice !== false) {
$finalPrice = min($finalPrice, $rulePrice);
}
break;
default:
if ($product->getFinalPrice() !== null) {
$price = $product->getPrice();
$finalPrice = $product->getFinalPrice();
$specialPrice = $product->getSpecialPrice();
} else {
$finalPrice = $product->getPriceInfo()->getPrice('final_price')->getValue();
$price = $product->getPriceInfo()->getPrice('regular_price')->getValue();
$product['min_price'] = $product->getPriceInfo()->getPrice('final_price')->getMinimalPrice()->getBaseAmount();
$product['max_price'] = $product->getPriceInfo()->getPrice('final_price')->getMaximalPrice()->getBaseAmount();
}
$rulePrice = $this->ruleFactory->create()->getRulePrice(
$config['timestamp'],
$config['website_id'],
'',
$product->getId()
);
if ($rulePrice !== null && $rulePrice !== false) {
$finalPrice = min($finalPrice, $rulePrice);
}
break;
}
$prices = [];
$attributes = $config['attributes'];
$config = $config['price_config'];
$prices[$config['price']] = $this->processPrice($product, $price, $config);
if (!empty($config['tax_include_both'])) {
$prices[$config['price_excl']] = $this->processPrice($product, $price, $config, false);
$prices[$config['price_incl']] = $this->processPrice($product, $price, $config, true);
}
if (isset($finalPrice) && !empty($config['final_price'])) {
$prices[$config['final_price']] = $this->processPrice($product, $finalPrice, $config);
}
if (isset($finalPrice) && ($price > $finalPrice) && !empty($config['sales_price'])) {
$prices[$config['sales_price']] = $this->processPrice($product, $finalPrice, $config);
if (!empty($config['tax_include_both'])) {
$prices[$config['sales_price_excl']] = $this->processPrice($product, $finalPrice, $config, false);
$prices[$config['sales_price_incl']] = $this->processPrice($product, $finalPrice, $config, true);
}
}
if (isset($specialPrice) && ($specialPrice == $finalPrice) && !empty($config['sales_date_range'])) {
if ($product->getSpecialFromDate() && $product->getSpecialToDate()) {
$from = date('Y-m-d', strtotime($product->getSpecialFromDate()));
$to = date('Y-m-d', strtotime($product->getSpecialToDate()));
$prices[$config['sales_date_range']] = $from . '/' . $to;
}
}
if ($price <= 0) {
if (!empty($product['min_price'])) {
$minPrice = $product['min_price'];
$prices[$config['price']] = $this->processPrice($product, $minPrice, $config);
if (!empty($config['tax_include_both'])) {
$prices[$config['price_excl']] = $this->processPrice($product, $minPrice, $config, false);
$prices[$config['price_incl']] = $this->processPrice($product, $minPrice, $config, true);
}
}
}
if (!empty($product['min_price']) && !empty($config['min_price'])) {
if (($finalPrice > 0) && $finalPrice < $product['min_price']) {
$prices[$config['min_price']] = $this->processPrice($product, $finalPrice, $config);
} else {
$prices[$config['min_price']] = $this->processPrice($product, $product['min_price'], $config);
}
}
if (!empty($product['max_price']) && !empty($config['max_price'])) {
$prices[$config['max_price']] = $this->processPrice($product, $product['max_price'], $config);
}
if (!empty($product['total_price']) && !empty($config['total_price'])) {
$prices[$config['total_price']] = $this->processPrice($product, $product['total_price'], $config);
}
if (!empty($config['discount_perc']) && isset($prices[$config['sales_price']])) {
if ($prices[$config['price']] > 0) {
$discount = ($prices[$config['sales_price']] - $prices[$config['price']]) / $prices[$config['price']];
$discount = $discount * -100;
if ($discount > 0) {
$prices[$config['discount_perc']] = round($discount, 1) . '%';
}
}
}
if ($extraRenderedPriceFields = preg_grep('/^rendered_price__/', array_keys($attributes))) {
foreach ($extraRenderedPriceFields as $label) {
$field = $attributes[$label];
$action = $field['actions'][0] ?? null;
$renderCurrency = ($action && strpos($action, 'currency_') === 0)
? explode('_', $action, 2)[1]
: null;
if ($renderCurrency !== null && $renderCurrency !== $config['currency']) {
$newConfig = $config;
$newConfig['currency'] = $renderCurrency;
$newConfig['exchange_rate'] = $config['exchange_rate_' . $renderCurrency] ?? 1;
switch ($field['price_source']) {
case 'price':
$prices[$field['label']] = $this->processPrice($product, $price, $newConfig);
break;
case 'min_price':
$prices[$field['label']] = $this->processPrice($product, $minPrice ?? $price, $newConfig);
break;
case 'max_price':
$prices[$field['label']] = $this->processPrice($product, $maxPrice ?? $price, $newConfig);
break;
}
} else {
$value = $prices[$field['price_source']] ?? null;
if ($action === 'round' && $value !== null) {
$value = preg_replace('/[\d.]+/', number_format(round((float)$value), 2, '.', ''), $value, 1);
}
$prices[$field['label']] = $value;
}
}
}
return $prices;
}
/**
* @param Product $product
* @param array $config
*
* @return array|null
*/
public function getGroupedPrices(Product $product, array $config): ?array
{
$subProducts = $product->getTypeInstance()->getAssociatedProducts($product);
$minPrice = null;
$maxPrice = null;
$totalPrice = null;
foreach ($subProducts as $subProduct) {
$subProduct->setWebsiteId($config['website_id']);
if ($subProduct->isSalable()) {
$price = $this->commonPriceModel->getCatalogPrice($subProduct);
if ($price < $minPrice || $minPrice === null) {
$minPrice = $this->commonPriceModel->getCatalogPrice($subProduct);
$product->setTaxClassId($subProduct->getTaxClassId());
}
if ($price > $maxPrice || $maxPrice === null) {
$maxPrice = $this->commonPriceModel->getCatalogPrice($subProduct);
$product->setTaxClassId($subProduct->getTaxClassId());
}
if ($subProduct->getQty() > 0) {
$totalPrice += $price * $subProduct->getQty();
} else {
$totalPrice += $price;
}
}
}
return ['min_price' => $minPrice, 'max_price' => $maxPrice, 'total_price' => $totalPrice];
}
/**
* @param Product $product
* @param $price
* @param array $config
* @param bool|null $includingTax
*
* @return string
*/
public function processPrice(Product $product, $price, array $config, ?bool $includingTax = null): string
{
if (!empty($config['exchange_rate'])) {
$price = $price * $config['exchange_rate'];
}
if ($includingTax !== null) {
return $this->formatPrice(
$this->catalogHelper->getTaxPrice($product, $price, $includingTax),
$config
);
}
if (isset($config['incl_vat'])) {
$price = $this->catalogHelper->getTaxPrice($product, $price, ['incl_vat']);
}
return $this->formatPrice($price, $config);
}
/**
* Format a price based on the provided configuration.
*
* @param float|string $price The price value to be formatted.
* @param array $config Configuration options for formatting.
* @return string The formatted price.
*/
public function formatPrice($price, array $config): string
{
$decimalPoint = $config['decimal_point'] ?? '.';
$currency = $config['currency'] ?? '';
$useCurrency = !empty($config['use_currency']);
// Ensure the price is converted to a float.
$formattedPrice = number_format(
floatval(str_replace(',', '.', (string)$price)),
2,
$decimalPoint,
''
);
// Append currency if configured.
if ($useCurrency && $formattedPrice >= 0) {
$formattedPrice .= ' ' . $currency;
}
return $formattedPrice;
}
/**
* Process tier pricing for a product.
*
* @param Product $product The product instance.
* @param array $config Configuration for price formatting.
* @return array|null An array of reformatted tier prices or null if none exist.
*/
public function processTierPrice(Product $product, array $config): ?array
{
if (!$tierPrices = $product->getData('tier_price')) {
return null;
}
$reformattedTierPrices = [];
foreach ($tierPrices as $priceTier) {
$price = !empty($priceTier['percentage_value'])
? $product->getPrice() * (1 - ($priceTier['percentage_value'] / 100)) // Subtract discount
: $priceTier['value'];
$reformattedTierPrices[] = [
'price_id' => $priceTier['value_id'] ?? null,
'website_id' => $priceTier['website_id'] ?? null,
'all_groups' => $priceTier['all_groups'] ?? 0,
'cust_group' => $priceTier['customer_group_id'] ?? null,
'qty' => $priceTier['qty'] ?? 0,
'price' => $this->formatPrice($price, $config)
];
}
return $reformattedTierPrices;
}
}