Skip to content

Commit 251002a

Browse files
committed
Merge remote-tracking branch 'origin/AC-13171' into spartans_pr_02122025
2 parents d96bb9d + 4b9edcb commit 251002a

File tree

7 files changed

+1328
-1
lines changed

7 files changed

+1328
-1
lines changed
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Weee\Plugin\ConfigurableProduct\Block\Product\View\Type;
9+
10+
use Magento\ConfigurableProduct\Block\Product\View\Type\Configurable as ConfigurableBlock;
11+
use Magento\Framework\Json\DecoderInterface;
12+
use Magento\Framework\Json\EncoderInterface;
13+
use Magento\Weee\Helper\Data as WeeeHelper;
14+
use Magento\Catalog\Model\Product;
15+
16+
/**
17+
* Plugin to add FPT data to configurable product JSON config
18+
*
19+
* @SuppressWarnings(PHPMD.StaticAccess)
20+
* phpcs:disable Magento2.Functions.StaticFunction
21+
*/
22+
class Configurable
23+
{
24+
/**
25+
* @param WeeeHelper $weeeHelper
26+
* @param EncoderInterface $jsonEncoder
27+
* @param DecoderInterface $jsonDecoder
28+
*/
29+
public function __construct(
30+
private readonly WeeeHelper $weeeHelper,
31+
private readonly EncoderInterface $jsonEncoder,
32+
private readonly DecoderInterface $jsonDecoder
33+
) {
34+
}
35+
36+
/**
37+
* Add FPT/WEEE data to option prices
38+
*
39+
* @param ConfigurableBlock $subject
40+
* @param string $result
41+
* @return string
42+
*/
43+
public function afterGetJsonConfig(
44+
ConfigurableBlock $subject,
45+
string $result
46+
): string {
47+
$config = $this->jsonDecoder->decode($result);
48+
49+
if (!$this->shouldProcessWeee($config)) {
50+
return $result;
51+
}
52+
53+
foreach ($subject->getAllowProducts() as $product) {
54+
$productId = (string)$product->getId();
55+
56+
if (!isset($config['optionPrices'][$productId])) {
57+
continue;
58+
}
59+
60+
$this->injectWeeeData($config, $productId, $product);
61+
}
62+
63+
return $this->jsonEncoder->encode($config);
64+
}
65+
66+
/**
67+
* Check if WEEE should be processed
68+
*
69+
* @param array|null $config
70+
* @return bool
71+
*/
72+
private function shouldProcessWeee(?array $config): bool
73+
{
74+
return !empty($config['optionPrices']) && $this->weeeHelper->isEnabled();
75+
}
76+
77+
/**
78+
* Inject processed WEEE data into config
79+
*
80+
* @param array $config
81+
* @param string $productId
82+
* @param Product $product
83+
* @return void
84+
*/
85+
private function injectWeeeData(array &$config, string $productId, Product $product): void
86+
{
87+
$attributes = $this->weeeHelper->getProductWeeeAttributesForDisplay($product);
88+
89+
if (empty($attributes)) {
90+
return;
91+
}
92+
93+
$weeeData = $this->processWeeeAttributes($attributes);
94+
95+
$this->appendFormattedWeee(
96+
$config['optionPrices'][$productId]['finalPrice'],
97+
$config['priceFormat'],
98+
$weeeData
99+
);
100+
}
101+
102+
/**
103+
* Convert raw attribute objects into array data
104+
*
105+
* @param array $weeeAttributes
106+
* @return array
107+
*/
108+
private function processWeeeAttributes(array $weeeAttributes): array
109+
{
110+
$processed = [];
111+
$total = 0.0;
112+
113+
foreach ($weeeAttributes as $attribute) {
114+
$amount = (float)$attribute->getAmount();
115+
$name = (string)($attribute->getData('name') ?: 'FPT');
116+
117+
$processed[] = [
118+
'name' => $name,
119+
'amount' => $amount,
120+
'amount_excl_tax' => (float)$attribute->getAmountExclTax(),
121+
'tax_amount' => (float)$attribute->getTaxAmount(),
122+
];
123+
124+
$total += $amount;
125+
}
126+
127+
return ['attributes' => $processed, 'total' => $total];
128+
}
129+
130+
/**
131+
* Add formatted WEEE data to price array
132+
*
133+
* @param array $finalPrice
134+
* @param array $priceFormat
135+
* @param array $weeeData
136+
* @return void
137+
*/
138+
private function appendFormattedWeee(
139+
array &$finalPrice,
140+
array $priceFormat,
141+
array $weeeData
142+
): void {
143+
$finalAmount = (float)$finalPrice['amount'];
144+
$baseAmount = $finalAmount - $weeeData['total'];
145+
146+
// Format each attribute
147+
$formattedAttrs = array_map(
148+
fn($attr) => [
149+
'name' => $attr['name'],
150+
'amount' => $attr['amount'],
151+
'formatted' => $this->formatPrice($attr['amount'], $priceFormat)
152+
],
153+
$weeeData['attributes']
154+
);
155+
156+
$finalPrice = array_merge(
157+
$finalPrice,
158+
[
159+
'weeeAmount' => $weeeData['total'],
160+
'weeeAttributes' => $formattedAttrs,
161+
'amountWithoutWeee' => $baseAmount,
162+
'formattedWithoutWeee' => $this->formatPrice($baseAmount, $priceFormat),
163+
'formattedWithWeee' => $this->formatPrice($finalAmount, $priceFormat),
164+
]
165+
);
166+
}
167+
168+
/**
169+
* Format price using the store's price format
170+
*
171+
* @param float $amount
172+
* @param array $priceFormat
173+
* @return string
174+
*/
175+
private function formatPrice(float $amount, array $priceFormat): string
176+
{
177+
$pattern = $priceFormat['pattern'] ?? '%s';
178+
$precision = $priceFormat['precision'] ?? 2;
179+
$decimalSymbol = $priceFormat['decimalSymbol'] ?? '.';
180+
$groupSymbol = $priceFormat['groupSymbol'] ?? ',';
181+
182+
return str_replace(
183+
'%s',
184+
number_format($amount, $precision, $decimalSymbol, $groupSymbol),
185+
$pattern
186+
);
187+
}
188+
}

0 commit comments

Comments
 (0)