6
6
7
7
namespace Magento \Catalog \Controller \Adminhtml \Product \Initialization ;
8
8
9
+ use DateTime ;
10
+ use Magento \Backend \Helper \Js ;
9
11
use Magento \Catalog \Api \Data \ProductCustomOptionInterfaceFactory as CustomOptionFactory ;
10
12
use Magento \Catalog \Api \Data \ProductLinkInterfaceFactory as ProductLinkFactory ;
13
+ use Magento \Catalog \Api \Data \ProductLinkTypeInterface ;
11
14
use Magento \Catalog \Api \ProductRepositoryInterface ;
12
15
use Magento \Catalog \Api \ProductRepositoryInterface \Proxy as ProductRepository ;
13
- use Magento \Catalog \Controller \Adminhtml \Product \Initialization \Helper \AttributeDefaultValueFilter ;
16
+ use Magento \Catalog \Controller \Adminhtml \Product \Initialization \Helper \AttributeFilter ;
14
17
use Magento \Catalog \Model \Product ;
15
18
use Magento \Catalog \Model \Product \Initialization \Helper \ProductLinks ;
16
19
use Magento \Catalog \Model \Product \Link \Resolver as LinkResolver ;
17
20
use Magento \Catalog \Model \Product \LinkTypeProvider ;
18
21
use Magento \Framework \App \ObjectManager ;
19
- use Magento \Catalog \Controller \Adminhtml \Product \Initialization \Helper \AttributeFilter ;
22
+ use Magento \Framework \App \RequestInterface ;
23
+ use Magento \Framework \Locale \FormatInterface ;
24
+ use Magento \Framework \Stdlib \DateTime \Filter \Date ;
25
+ use Magento \Store \Model \StoreManagerInterface ;
26
+ use Zend_Filter_Input ;
20
27
21
28
/**
22
29
* Product helper
28
35
class Helper
29
36
{
30
37
/**
31
- * @var \Magento\Framework\App\ RequestInterface
38
+ * @var RequestInterface
32
39
*/
33
40
protected $ request ;
34
41
35
42
/**
36
- * @var \Magento\Store\Model\ StoreManagerInterface
43
+ * @var StoreManagerInterface
37
44
*/
38
45
protected $ storeManager ;
39
46
@@ -43,12 +50,12 @@ class Helper
43
50
protected $ stockFilter ;
44
51
45
52
/**
46
- * @var \Magento\Backend\Helper\ Js
53
+ * @var Js
47
54
*/
48
55
protected $ jsHelper ;
49
56
50
57
/**
51
- * @var \Magento\Framework\Stdlib\DateTime\Filter\ Date
58
+ * @var Date
52
59
* @deprecated 101.0.0
53
60
*/
54
61
protected $ dateFilter ;
@@ -96,34 +103,41 @@ class Helper
96
103
*/
97
104
private $ attributeFilter ;
98
105
106
+ /**
107
+ * @var FormatInterface
108
+ */
109
+ private $ localeFormat ;
110
+
99
111
/**
100
112
* Constructor
101
113
*
102
- * @param \Magento\Framework\App\ RequestInterface $request
103
- * @param \Magento\Store\Model\ StoreManagerInterface $storeManager
114
+ * @param RequestInterface $request
115
+ * @param StoreManagerInterface $storeManager
104
116
* @param StockDataFilter $stockFilter
105
117
* @param ProductLinks $productLinks
106
- * @param \Magento\Backend\Helper\ Js $jsHelper
107
- * @param \Magento\Framework\Stdlib\DateTime\Filter\ Date $dateFilter
118
+ * @param Js $jsHelper
119
+ * @param Date $dateFilter
108
120
* @param CustomOptionFactory|null $customOptionFactory
109
121
* @param ProductLinkFactory|null $productLinkFactory
110
122
* @param ProductRepositoryInterface|null $productRepository
111
123
* @param LinkTypeProvider|null $linkTypeProvider
112
124
* @param AttributeFilter|null $attributeFilter
125
+ * @param FormatInterface|null $localeFormat
113
126
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
114
127
*/
115
128
public function __construct (
116
- \ Magento \ Framework \ App \ RequestInterface $ request ,
117
- \ Magento \ Store \ Model \ StoreManagerInterface $ storeManager ,
129
+ RequestInterface $ request ,
130
+ StoreManagerInterface $ storeManager ,
118
131
StockDataFilter $ stockFilter ,
119
- \ Magento \ Catalog \ Model \ Product \ Initialization \ Helper \ ProductLinks $ productLinks ,
120
- \ Magento \ Backend \ Helper \ Js $ jsHelper ,
121
- \ Magento \ Framework \ Stdlib \ DateTime \ Filter \ Date $ dateFilter ,
132
+ ProductLinks $ productLinks ,
133
+ Js $ jsHelper ,
134
+ Date $ dateFilter ,
122
135
CustomOptionFactory $ customOptionFactory = null ,
123
136
ProductLinkFactory $ productLinkFactory = null ,
124
137
ProductRepositoryInterface $ productRepository = null ,
125
138
LinkTypeProvider $ linkTypeProvider = null ,
126
- AttributeFilter $ attributeFilter = null
139
+ AttributeFilter $ attributeFilter = null ,
140
+ FormatInterface $ localeFormat = null
127
141
) {
128
142
$ this ->request = $ request ;
129
143
$ this ->storeManager = $ storeManager ;
@@ -132,26 +146,27 @@ public function __construct(
132
146
$ this ->jsHelper = $ jsHelper ;
133
147
$ this ->dateFilter = $ dateFilter ;
134
148
135
- $ objectManager = \ Magento \ Framework \ App \ ObjectManager::getInstance ();
149
+ $ objectManager = ObjectManager::getInstance ();
136
150
$ this ->customOptionFactory = $ customOptionFactory ?: $ objectManager ->get (CustomOptionFactory::class);
137
151
$ this ->productLinkFactory = $ productLinkFactory ?: $ objectManager ->get (ProductLinkFactory::class);
138
152
$ this ->productRepository = $ productRepository ?: $ objectManager ->get (ProductRepositoryInterface::class);
139
153
$ this ->linkTypeProvider = $ linkTypeProvider ?: $ objectManager ->get (LinkTypeProvider::class);
140
154
$ this ->attributeFilter = $ attributeFilter ?: $ objectManager ->get (AttributeFilter::class);
155
+ $ this ->localeFormat = $ localeFormat ?: $ objectManager ->get (FormatInterface::class);
141
156
}
142
157
143
158
/**
144
159
* Initialize product from data
145
160
*
146
- * @param \Magento\Catalog\Model\ Product $product
161
+ * @param Product $product
147
162
* @param array $productData
148
- * @return \Magento\Catalog\Model\ Product
163
+ * @return Product
149
164
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
150
165
* @SuppressWarnings(PHPMD.NPathComplexity)
151
166
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
152
167
* @since 101.0.0
153
168
*/
154
- public function initializeFromData (\ Magento \ Catalog \ Model \ Product $ product , array $ productData )
169
+ public function initializeFromData (Product $ product , array $ productData )
155
170
{
156
171
unset($ productData ['custom_attributes ' ], $ productData ['extension_attributes ' ]);
157
172
@@ -190,7 +205,7 @@ public function initializeFromData(\Magento\Catalog\Model\Product $product, arra
190
205
}
191
206
}
192
207
193
- $ inputFilter = new \ Zend_Filter_Input ($ dateFieldFilters , [], $ productData );
208
+ $ inputFilter = new Zend_Filter_Input ($ dateFieldFilters , [], $ productData );
194
209
$ productData = $ inputFilter ->getUnescaped ();
195
210
196
211
if (isset ($ productData ['options ' ])) {
@@ -201,7 +216,7 @@ public function initializeFromData(\Magento\Catalog\Model\Product $product, arra
201
216
}
202
217
$ productData ['tier_price ' ] = isset ($ productData ['tier_price ' ]) ? $ productData ['tier_price ' ] : [];
203
218
204
- $ useDefaults = (array )$ this ->request ->getPost ('use_default ' , []);
219
+ $ useDefaults = (array ) $ this ->request ->getPost ('use_default ' , []);
205
220
$ productData = $ this ->attributeFilter ->prepareProductAttributes ($ product , $ productData , $ useDefaults );
206
221
$ product ->addData ($ productData );
207
222
@@ -222,24 +237,25 @@ public function initializeFromData(\Magento\Catalog\Model\Product $product, arra
222
237
/**
223
238
* Initialize product before saving
224
239
*
225
- * @param \Magento\Catalog\Model\ Product $product
226
- * @return \Magento\Catalog\Model\ Product
240
+ * @param Product $product
241
+ * @return Product
227
242
*/
228
- public function initialize (\ Magento \ Catalog \ Model \ Product $ product )
243
+ public function initialize (Product $ product )
229
244
{
230
245
$ productData = $ this ->request ->getPost ('product ' , []);
246
+
231
247
return $ this ->initializeFromData ($ product , $ productData );
232
248
}
233
249
234
250
/**
235
251
* Setting product links
236
252
*
237
- * @param \Magento\Catalog\Model\ Product $product
238
- * @return \Magento\Catalog\Model\ Product
253
+ * @param Product $product
254
+ * @return Product
239
255
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
240
256
* @since 101.0.0
241
257
*/
242
- protected function setProductLinks (\ Magento \ Catalog \ Model \ Product $ product )
258
+ protected function setProductLinks (Product $ product )
243
259
{
244
260
$ links = $ this ->getLinkResolver ()->getLinks ();
245
261
@@ -249,7 +265,7 @@ protected function setProductLinks(\Magento\Catalog\Model\Product $product)
249
265
$ productLinks = $ product ->getProductLinks ();
250
266
$ linkTypes = [];
251
267
252
- /** @var \Magento\Catalog\Api\Data\ ProductLinkTypeInterface $linkTypeObject */
268
+ /** @var ProductLinkTypeInterface $linkTypeObject */
253
269
foreach ($ this ->linkTypeProvider ->getItems () as $ linkTypeObject ) {
254
270
$ linkTypes [$ linkTypeObject ->getName ()] = $ product ->getData ($ linkTypeObject ->getName () . '_readonly ' );
255
271
}
@@ -261,7 +277,7 @@ protected function setProductLinks(\Magento\Catalog\Model\Product $product)
261
277
262
278
foreach ($ linkTypes as $ linkType => $ readonly ) {
263
279
if (isset ($ links [$ linkType ]) && !$ readonly ) {
264
- foreach ((array )$ links [$ linkType ] as $ linkData ) {
280
+ foreach ((array ) $ links [$ linkType ] as $ linkData ) {
265
281
if (empty ($ linkData ['id ' ])) {
266
282
continue ;
267
283
}
@@ -271,7 +287,7 @@ protected function setProductLinks(\Magento\Catalog\Model\Product $product)
271
287
$ link ->setSku ($ product ->getSku ())
272
288
->setLinkedProductSku ($ linkProduct ->getSku ())
273
289
->setLinkType ($ linkType )
274
- ->setPosition (isset ($ linkData ['position ' ]) ? (int )$ linkData ['position ' ] : 0 );
290
+ ->setPosition (isset ($ linkData ['position ' ]) ? (int ) $ linkData ['position ' ] : 0 );
275
291
$ productLinks [] = $ link ;
276
292
}
277
293
}
@@ -377,6 +393,7 @@ private function getLinkResolver()
377
393
if (!is_object ($ this ->linkResolver )) {
378
394
$ this ->linkResolver = ObjectManager::getInstance ()->get (LinkResolver::class);
379
395
}
396
+
380
397
return $ this ->linkResolver ;
381
398
}
382
399
@@ -389,9 +406,10 @@ private function getLinkResolver()
389
406
private function getDateTimeFilter ()
390
407
{
391
408
if ($ this ->dateTimeFilter === null ) {
392
- $ this ->dateTimeFilter = \ Magento \ Framework \ App \ ObjectManager::getInstance ()
409
+ $ this ->dateTimeFilter = ObjectManager::getInstance ()
393
410
->get (\Magento \Framework \Stdlib \DateTime \Filter \DateTime::class);
394
411
}
412
+
395
413
return $ this ->dateTimeFilter ;
396
414
}
397
415
@@ -407,7 +425,7 @@ private function getDateTimeFilter()
407
425
private function filterWebsiteIds ($ websiteIds )
408
426
{
409
427
if (!$ this ->storeManager ->isSingleStoreMode ()) {
410
- $ websiteIds = array_filter ((array )$ websiteIds );
428
+ $ websiteIds = array_filter ((array ) $ websiteIds );
411
429
} else {
412
430
$ websiteIds [$ this ->storeManager ->getWebsite (true )->getId ()] = 1 ;
413
431
}
@@ -448,9 +466,17 @@ private function fillProductOptions(Product $product, array $productOptions)
448
466
}
449
467
450
468
if (isset ($ customOptionData ['values ' ])) {
451
- $ customOptionData ['values ' ] = array_filter ($ customOptionData ['values ' ], function ($ valueData ) {
452
- return empty ($ valueData ['is_delete ' ]);
453
- });
469
+ $ customOptionData ['values ' ] = array_filter (
470
+ $ customOptionData ['values ' ],
471
+ function ($ valueData ) {
472
+ return empty ($ valueData ['is_delete ' ]);
473
+ }
474
+ );
475
+ }
476
+
477
+ if (isset ($ customOptionData ['price ' ])) {
478
+ // Make sure we're working with a number here and no localized value.
479
+ $ customOptionData ['price ' ] = $ this ->localeFormat ->getNumber ($ customOptionData ['price ' ]);
454
480
}
455
481
456
482
$ customOption = $ this ->customOptionFactory ->create (['data ' => $ customOptionData ]);
@@ -471,7 +497,7 @@ private function convertSpecialFromDateStringToObject($productData)
471
497
{
472
498
if (isset ($ productData ['special_from_date ' ]) && $ productData ['special_from_date ' ] != '' ) {
473
499
$ productData ['special_from_date ' ] = $ this ->getDateTimeFilter ()->filter ($ productData ['special_from_date ' ]);
474
- $ productData ['special_from_date ' ] = new \ DateTime ($ productData ['special_from_date ' ]);
500
+ $ productData ['special_from_date ' ] = new DateTime ($ productData ['special_from_date ' ]);
475
501
}
476
502
477
503
return $ productData ;
0 commit comments