Skip to content

Commit 5faff7f

Browse files
committed
AC-13171: Fixed Product Tax (FPT) is not displaying separately with configurable products
Fix for static and health failures
1 parent 1cac3f7 commit 5faff7f

File tree

2 files changed

+106
-58
lines changed

2 files changed

+106
-58
lines changed

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

Lines changed: 105 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
/**
1616
* Plugin to add FPT data to configurable product JSON config
17+
*
18+
* @SuppressWarnings(PHPMD.StaticAccess)
19+
* phpcs:disable Magento2.Functions.StaticFunction
1720
*/
1821
class Configurable
1922
{
@@ -42,7 +45,6 @@ private function formatPrice(float $amount, array $priceFormat): string
4245
$precision = $priceFormat['precision'] ?? 2;
4346
$decimalSymbol = $priceFormat['decimalSymbol'] ?? '.';
4447
$groupSymbol = $priceFormat['groupSymbol'] ?? ',';
45-
$groupLength = $priceFormat['groupLength'] ?? 3;
4648

4749
$formatted = number_format($amount, $precision, $decimalSymbol, $groupSymbol);
4850
return str_replace('%s', $formatted, $pattern);
@@ -61,11 +63,7 @@ public function afterGetJsonConfig(
6163
): string {
6264
$config = $this->jsonDecoder->decode($result);
6365

64-
if (!$config || !isset($config['optionPrices'])) {
65-
return $result;
66-
}
67-
68-
if (!$this->weeeHelper->isEnabled()) {
66+
if (!$this->shouldProcessWeee($config)) {
6967
return $result;
7068
}
7169

@@ -76,59 +74,109 @@ public function afterGetJsonConfig(
7674
continue;
7775
}
7876

79-
// Get FPT attributes for this product
80-
$weeeAttributes = $this->weeeHelper->getProductWeeeAttributesForDisplay($product);
81-
82-
// Add FPT data to the option price
83-
$config['optionPrices'][$productId]['weeeAttributes'] = [];
84-
85-
if (!empty($weeeAttributes)) {
86-
$weeeTotal = 0;
87-
foreach ($weeeAttributes as $attribute) {
88-
// Use getData('name') which contains the frontend label (label_value or frontend_label)
89-
// This is set by \Magento\Weee\Model\Tax::getProductWeeeAttributes() at line 374-376
90-
$name = $attribute->getData('name');
91-
92-
// Cast to string to handle Magento\Framework\Phrase objects
93-
$name = $name ? (string)$name : 'FPT';
94-
95-
$config['optionPrices'][$productId]['weeeAttributes'][] = [
96-
'name' => $name,
97-
'amount' => (float)$attribute->getAmount(),
98-
'amount_excl_tax' => (float)$attribute->getAmountExclTax(),
99-
'tax_amount' => (float)$attribute->getTaxAmount(),
100-
];
101-
102-
$weeeTotal += (float)$attribute->getAmount();
103-
}
104-
105-
// Calculate base price without WEEE
106-
$basePriceAmount = $config['optionPrices'][$productId]['finalPrice']['amount'] - $weeeTotal;
107-
108-
// Format WEEE amounts for display
109-
$formattedWeeeAttributes = [];
110-
foreach ($config['optionPrices'][$productId]['weeeAttributes'] as $weeeAttr) {
111-
$formattedWeeeAttributes[] = [
112-
'name' => $weeeAttr['name'],
113-
'amount' => $weeeAttr['amount'],
114-
'formatted' => $this->formatPrice($weeeAttr['amount'], $config['priceFormat'])
115-
];
116-
}
117-
118-
// Store WEEE data in finalPrice object (will be used by price-box reloadPrice)
119-
$config['optionPrices'][$productId]['finalPrice']['weeeAmount'] = $weeeTotal;
120-
$config['optionPrices'][$productId]['finalPrice']['weeeAttributes'] = $formattedWeeeAttributes;
121-
$config['optionPrices'][$productId]['finalPrice']['amountWithoutWeee'] = $basePriceAmount;
122-
$config['optionPrices'][$productId]['finalPrice']['formattedWithoutWeee'] =
123-
$this->formatPrice($basePriceAmount, $config['priceFormat']);
124-
$config['optionPrices'][$productId]['finalPrice']['formattedWithWeee'] =
125-
$this->formatPrice(
126-
$config['optionPrices'][$productId]['finalPrice']['amount'],
127-
$config['priceFormat']
128-
);
129-
}
77+
$this->addWeeeDataToProduct($config, $productId, $product);
13078
}
13179

13280
return $this->jsonEncoder->encode($config);
13381
}
82+
83+
/**
84+
* Check if WEEE should be processed
85+
*
86+
* @param array|null $config
87+
* @return bool
88+
*/
89+
private function shouldProcessWeee(?array $config): bool
90+
{
91+
return $config
92+
&& isset($config['optionPrices'])
93+
&& $this->weeeHelper->isEnabled();
94+
}
95+
96+
/**
97+
* Add WEEE data to product option price
98+
*
99+
* @param array $config
100+
* @param string $productId
101+
* @param \Magento\Catalog\Model\Product $product
102+
* @return void
103+
*/
104+
private function addWeeeDataToProduct(array &$config, string $productId, $product): void
105+
{
106+
$weeeAttributes = $this->weeeHelper->getProductWeeeAttributesForDisplay($product);
107+
$config['optionPrices'][$productId]['weeeAttributes'] = [];
108+
109+
if (empty($weeeAttributes)) {
110+
return;
111+
}
112+
113+
$weeeData = $this->processWeeeAttributes($weeeAttributes);
114+
$this->addFormattedWeeeData($config, $productId, $weeeData);
115+
}
116+
117+
/**
118+
* Process WEEE attributes and calculate total
119+
*
120+
* @param array $weeeAttributes
121+
* @return array
122+
*/
123+
private function processWeeeAttributes(array $weeeAttributes): array
124+
{
125+
$processedAttributes = [];
126+
$weeeTotal = 0;
127+
128+
foreach ($weeeAttributes as $attribute) {
129+
$name = $attribute->getData('name');
130+
$name = $name ? (string)$name : 'FPT';
131+
$amount = (float)$attribute->getAmount();
132+
133+
$processedAttributes[] = [
134+
'name' => $name,
135+
'amount' => $amount,
136+
'amount_excl_tax' => (float)$attribute->getAmountExclTax(),
137+
'tax_amount' => (float)$attribute->getTaxAmount(),
138+
];
139+
140+
$weeeTotal += $amount;
141+
}
142+
143+
return [
144+
'attributes' => $processedAttributes,
145+
'total' => $weeeTotal
146+
];
147+
}
148+
149+
/**
150+
* Add formatted WEEE data to config
151+
*
152+
* @param array $config
153+
* @param string $productId
154+
* @param array $weeeData
155+
* @return void
156+
*/
157+
private function addFormattedWeeeData(array &$config, string $productId, array $weeeData): void
158+
{
159+
$config['optionPrices'][$productId]['weeeAttributes'] = $weeeData['attributes'];
160+
$basePriceAmount = $config['optionPrices'][$productId]['finalPrice']['amount'] - $weeeData['total'];
161+
162+
$formattedWeeeAttributes = [];
163+
foreach ($weeeData['attributes'] as $weeeAttr) {
164+
$formattedWeeeAttributes[] = [
165+
'name' => $weeeAttr['name'],
166+
'amount' => $weeeAttr['amount'],
167+
'formatted' => $this->formatPrice($weeeAttr['amount'], $config['priceFormat'])
168+
];
169+
}
170+
171+
$config['optionPrices'][$productId]['finalPrice']['weeeAmount'] = $weeeData['total'];
172+
$config['optionPrices'][$productId]['finalPrice']['weeeAttributes'] = $formattedWeeeAttributes;
173+
$config['optionPrices'][$productId]['finalPrice']['amountWithoutWeee'] = $basePriceAmount;
174+
$config['optionPrices'][$productId]['finalPrice']['formattedWithoutWeee'] =
175+
$this->formatPrice($basePriceAmount, $config['priceFormat']);
176+
$config['optionPrices'][$productId]['finalPrice']['formattedWithWeee'] =
177+
$this->formatPrice(
178+
$config['optionPrices'][$productId]['finalPrice']['amount'],
179+
$config['priceFormat']
180+
);
181+
}
134182
}

app/code/Magento/Weee/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"magento/module-ui": "*"
2323
},
2424
"suggest": {
25-
"magento/module-bundle": "*"
25+
"magento/module-swatches": "*"
2626
},
2727
"type": "magento2-module",
2828
"license": [

0 commit comments

Comments
 (0)