8
8
namespace Magento \Checkout \Model \Cart ;
9
9
10
10
use Magento \Framework \Locale \ResolverInterface ;
11
+ use NumberFormatter ;
11
12
12
13
/**
13
14
* Cart request quantity processor
@@ -37,13 +38,11 @@ public function __construct(
37
38
*/
38
39
public function process (array $ cartData ): array
39
40
{
40
- $ filter = new \Laminas \I18n \Filter \NumberParse ($ this ->localeResolver ->getLocale ());
41
-
42
41
foreach ($ cartData as $ index => $ data ) {
43
42
if (isset ($ data ['qty ' ])) {
44
43
$ data ['qty ' ] = $ this ->prepareQuantity ($ data ['qty ' ]);
45
44
$ data ['qty ' ] = is_string ($ data ['qty ' ]) ? trim ($ data ['qty ' ]) : $ data ['qty ' ];
46
- $ cartData [$ index ]['qty ' ] = $ filter ->filter ($ data ['qty ' ]);
45
+ $ cartData [$ index ]['qty ' ] = $ this ->filter ($ data ['qty ' ]);
47
46
}
48
47
}
49
48
@@ -55,12 +54,11 @@ public function process(array $cartData): array
55
54
*
56
55
* @param int|float|string|array $quantity
57
56
* @return int|float|string|array
58
- * @throws \Zend_Locale_Exception
59
57
*/
60
58
public function prepareQuantity ($ quantity )
61
59
{
62
- $ formatter = new \ NumberFormatter ($ this ->localeResolver ->getLocale (), \ NumberFormatter::CURRENCY );
63
- $ decimalSymbol = $ formatter ->getSymbol (\ NumberFormatter::DECIMAL_SEPARATOR_SYMBOL );
60
+ $ formatter = new NumberFormatter ($ this ->localeResolver ->getLocale (), NumberFormatter::CURRENCY );
61
+ $ decimalSymbol = $ formatter ->getSymbol (NumberFormatter::DECIMAL_SEPARATOR_SYMBOL );
64
62
65
63
if (is_array ($ quantity )) {
66
64
foreach ($ quantity as $ key => $ qty ) {
@@ -70,8 +68,47 @@ public function prepareQuantity($quantity)
70
68
}
71
69
} else {
72
70
if (strpos ((string )$ quantity , '. ' ) !== false && $ decimalSymbol !== '. ' ) {
73
- $ quantity = str_replace ('. ' , $ decimalSymbol , (string )$ quantity );
71
+ $ quantity = str_replace ('. ' , $ decimalSymbol , (string ) $ quantity );
72
+ }
73
+ }
74
+
75
+ return $ quantity ;
76
+ }
77
+
78
+ /**
79
+ * Filter quantity value and parse it by region if needed.
80
+ *
81
+ * @param float|int|array|string $quantity
82
+ *
83
+ * @return float|int|array|string
84
+ */
85
+ private function filter ($ quantity )
86
+ {
87
+ $ formatter = new NumberFormatter ($ this ->localeResolver ->getLocale (), NumberFormatter::DEFAULT_STYLE );
88
+
89
+ if (is_array ($ quantity )) {
90
+ foreach ($ quantity as $ key => $ qty ) {
91
+ $ quantity [$ key ] = $ this ->parseFormat ($ qty , $ formatter );
74
92
}
93
+ } else {
94
+ $ quantity = $ this ->parseFormat ($ quantity , $ formatter );
95
+ }
96
+
97
+ return $ quantity ;
98
+ }
99
+
100
+ /**
101
+ * Phrase quantity value if needed.
102
+ *
103
+ * @param float|int|string $quantity
104
+ * @param NumberFormatter $formatter
105
+ *
106
+ * @return float|int|string
107
+ */
108
+ private function parseFormat ($ quantity , NumberFormatter $ formatter )
109
+ {
110
+ if (!is_float ($ quantity ) && !is_int ($ quantity )) {
111
+ return $ formatter ->parse ($ quantity );
75
112
}
76
113
77
114
return $ quantity ;
0 commit comments