Skip to content

Commit 2ba87a8

Browse files
committed
AC-13171: Fixed Product Tax (FPT) is not displaying separately with configurable products
Added unit and integration test coverage, updated js file to fix static test eslint errors
1 parent eca0128 commit 2ba87a8

File tree

4 files changed

+984
-94
lines changed

4 files changed

+984
-94
lines changed

app/code/Magento/Weee/Plugin/ConfigurableProduct/Block/Product/View/Type/Configurable.php

Lines changed: 62 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\Json\DecoderInterface;
1212
use Magento\Framework\Json\EncoderInterface;
1313
use Magento\Weee\Helper\Data as WeeeHelper;
14+
use Magento\Catalog\Model\Product;
1415

1516
/**
1617
* Plugin to add FPT data to configurable product JSON config
@@ -20,24 +21,14 @@
2021
*/
2122
class Configurable
2223
{
23-
/**
24-
* @param WeeeHelper $weeeHelper
25-
* @param EncoderInterface $jsonEncoder
26-
* @param DecoderInterface $jsonDecoder
27-
*/
2824
public function __construct(
2925
private readonly WeeeHelper $weeeHelper,
3026
private readonly EncoderInterface $jsonEncoder,
3127
private readonly DecoderInterface $jsonDecoder
32-
) {
33-
}
28+
) {}
3429

3530
/**
36-
* Add FPT data to option prices
37-
*
38-
* @param ConfigurableBlock $subject
39-
* @param string $result
40-
* @return string
31+
* Add FPT/WEEE data to option prices
4132
*/
4233
public function afterGetJsonConfig(
4334
ConfigurableBlock $subject,
@@ -56,123 +47,112 @@ public function afterGetJsonConfig(
5647
continue;
5748
}
5849

59-
$this->addWeeeDataToProduct($config, $productId, $product);
50+
$this->injectWeeeData($config, $productId, $product);
6051
}
6152

6253
return $this->jsonEncoder->encode($config);
6354
}
6455

6556
/**
6657
* Check if WEEE should be processed
67-
*
68-
* @param array|null $config
69-
* @return bool
7058
*/
7159
private function shouldProcessWeee(?array $config): bool
7260
{
73-
return $config
74-
&& isset($config['optionPrices'])
75-
&& $this->weeeHelper->isEnabled();
61+
return !empty($config['optionPrices']) && $this->weeeHelper->isEnabled();
7662
}
7763

7864
/**
79-
* Add WEEE data to product option price
80-
*
81-
* @param array $config
82-
* @param string $productId
83-
* @param \Magento\Catalog\Model\Product $product
84-
* @return void
65+
* Inject processed WEEE data into config
8566
*/
86-
private function addWeeeDataToProduct(array &$config, string $productId, $product): void
67+
private function injectWeeeData(array &$config, string $productId, Product $product): void
8768
{
88-
$weeeAttributes = $this->weeeHelper->getProductWeeeAttributesForDisplay($product);
69+
$attributes = $this->weeeHelper->getProductWeeeAttributesForDisplay($product);
8970

90-
if (empty($weeeAttributes)) {
71+
if (empty($attributes)) {
9172
return;
9273
}
9374

94-
$weeeData = $this->processWeeeAttributes($weeeAttributes);
95-
$this->addFormattedWeeeData($config, $productId, $weeeData);
75+
$weeeData = $this->processWeeeAttributes($attributes);
76+
77+
$this->appendFormattedWeee(
78+
$config['optionPrices'][$productId]['finalPrice'],
79+
$config['priceFormat'],
80+
$weeeData
81+
);
9682
}
9783

9884
/**
99-
* Process WEEE attributes and calculate total
100-
*
101-
* @param array $weeeAttributes
102-
* @return array
85+
* Convert raw attribute objects into array data
10386
*/
10487
private function processWeeeAttributes(array $weeeAttributes): array
10588
{
106-
$processedAttributes = [];
107-
$weeeTotal = 0;
89+
$processed = [];
90+
$total = 0.0;
10891

10992
foreach ($weeeAttributes as $attribute) {
110-
$name = $attribute->getData('name');
111-
$name = $name ? (string)$name : 'FPT';
11293
$amount = (float)$attribute->getAmount();
94+
$name = (string)($attribute->getData('name') ?: 'FPT');
11395

114-
$processedAttributes[] = [
96+
$processed[] = [
11597
'name' => $name,
11698
'amount' => $amount,
11799
'amount_excl_tax' => (float)$attribute->getAmountExclTax(),
118100
'tax_amount' => (float)$attribute->getTaxAmount(),
119101
];
120102

121-
$weeeTotal += $amount;
103+
$total += $amount;
122104
}
123105

124-
return [
125-
'attributes' => $processedAttributes,
126-
'total' => $weeeTotal
127-
];
106+
return ['attributes' => $processed, 'total' => $total];
128107
}
129108

130109
/**
131-
* Add formatted WEEE data to config
132-
*
133-
* @param array $config
134-
* @param string $productId
135-
* @param array $weeeData
136-
* @return void
110+
* Add formatted WEEE data to price array
137111
*/
138-
private function addFormattedWeeeData(array &$config, string $productId, array $weeeData): void
139-
{
140-
$finalPriceAmount = $config['optionPrices'][$productId]['finalPrice']['amount'];
141-
$basePriceAmount = $finalPriceAmount - $weeeData['total'];
142-
143-
$formattedWeeeAttributes = [];
144-
foreach ($weeeData['attributes'] as $weeeAttr) {
145-
$formattedWeeeAttributes[] = [
146-
'name' => $weeeAttr['name'],
147-
'amount' => $weeeAttr['amount'],
148-
'formatted' => $this->formatPrice($weeeAttr['amount'], $config['priceFormat'])
149-
];
150-
}
151-
152-
$config['optionPrices'][$productId]['finalPrice']['weeeAmount'] = $weeeData['total'];
153-
$config['optionPrices'][$productId]['finalPrice']['weeeAttributes'] = $formattedWeeeAttributes;
154-
$config['optionPrices'][$productId]['finalPrice']['amountWithoutWeee'] = $basePriceAmount;
155-
$config['optionPrices'][$productId]['finalPrice']['formattedWithoutWeee'] =
156-
$this->formatPrice($basePriceAmount, $config['priceFormat']);
157-
$config['optionPrices'][$productId]['finalPrice']['formattedWithWeee'] =
158-
$this->formatPrice($finalPriceAmount, $config['priceFormat']);
112+
private function appendFormattedWeee(
113+
array &$finalPrice,
114+
array $priceFormat,
115+
array $weeeData
116+
): void {
117+
$finalAmount = (float)$finalPrice['amount'];
118+
$baseAmount = $finalAmount - $weeeData['total'];
119+
120+
// Format each attribute
121+
$formattedAttrs = array_map(
122+
fn($attr) => [
123+
'name' => $attr['name'],
124+
'amount' => $attr['amount'],
125+
'formatted' => $this->formatPrice($attr['amount'], $priceFormat)
126+
],
127+
$weeeData['attributes']
128+
);
129+
130+
$finalPrice = array_merge(
131+
$finalPrice,
132+
[
133+
'weeeAmount' => $weeeData['total'],
134+
'weeeAttributes' => $formattedAttrs,
135+
'amountWithoutWeee' => $baseAmount,
136+
'formattedWithoutWeee' => $this->formatPrice($baseAmount, $priceFormat),
137+
'formattedWithWeee' => $this->formatPrice($finalAmount, $priceFormat),
138+
]
139+
);
159140
}
160141

161142
/**
162143
* Format price using the store's price format
163-
*
164-
* @param float $amount
165-
* @param array $priceFormat
166-
* @return string
167144
*/
168145
private function formatPrice(float $amount, array $priceFormat): string
169146
{
170-
$pattern = $priceFormat['pattern'] ?? '%s';
171-
$precision = $priceFormat['precision'] ?? 2;
172-
$decimalSymbol = $priceFormat['decimalSymbol'] ?? '.';
173-
$groupSymbol = $priceFormat['groupSymbol'] ?? ',';
174-
175-
$formatted = number_format($amount, $precision, $decimalSymbol, $groupSymbol);
176-
return str_replace('%s', $formatted, $pattern);
147+
$pattern = $priceFormat['pattern'] ?? '%s';
148+
$precision = $priceFormat['precision'] ?? 2;
149+
$decimalSymbol = $priceFormat['decimalSymbol'] ?? '.';
150+
$groupSymbol = $priceFormat['groupSymbol'] ?? ',';
151+
152+
return str_replace(
153+
'%s',
154+
number_format($amount, $precision, $decimalSymbol, $groupSymbol),
155+
$pattern
156+
);
177157
}
178158
}

0 commit comments

Comments
 (0)