8
8
use Magento \Catalog \Api \Data \ProductCustomOptionInterfaceFactory as CustomOptionFactory ;
9
9
use Magento \Catalog \Api \Data \ProductLinkInterfaceFactory as ProductLinkFactory ;
10
10
use Magento \Catalog \Api \ProductRepositoryInterface \Proxy as ProductRepository ;
11
+ use Magento \Catalog \Model \Product ;
11
12
use Magento \Catalog \Model \Product \Initialization \Helper \ProductLinks ;
12
13
use Magento \Catalog \Model \Product \Link \Resolver as LinkResolver ;
13
14
use Magento \Framework \App \ObjectManager ;
@@ -112,14 +113,14 @@ public function __construct(
112
113
/**
113
114
* Initialize product from data
114
115
*
115
- * @param \Magento\Catalog\Model\ Product $product
116
+ * @param Product $product
116
117
* @param array $productData
117
- * @return \Magento\Catalog\Model\ Product
118
+ * @return Product
118
119
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
119
120
* @SuppressWarnings(PHPMD.NPathComplexity)
120
121
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
121
122
*/
122
- public function initializeFromData (\ Magento \ Catalog \ Model \ Product $ product , array $ productData )
123
+ public function initializeFromData (Product $ product , array $ productData )
123
124
{
124
125
unset($ productData ['custom_attributes ' ]);
125
126
unset($ productData ['extension_attributes ' ]);
@@ -195,36 +196,7 @@ public function initializeFromData(\Magento\Catalog\Model\Product $product, arra
195
196
}
196
197
197
198
$ product = $ this ->setProductLinks ($ product );
198
-
199
- /**
200
- * Initialize product options
201
- */
202
- if ($ productOptions && !$ product ->getOptionsReadonly ()) {
203
- // mark custom options that should to fall back to default value
204
- $ options = $ this ->mergeProductOptions (
205
- $ productOptions ,
206
- $ this ->request ->getPost ('options_use_default ' )
207
- );
208
- $ customOptions = [];
209
- foreach ($ options as $ customOptionData ) {
210
- if (empty ($ customOptionData ['is_delete ' ])) {
211
- if (empty ($ customOptionData ['option_id ' ])) {
212
- $ customOptionData ['option_id ' ] = null ;
213
- }
214
-
215
- if (isset ($ customOptionData ['values ' ])) {
216
- $ customOptionData ['values ' ] = array_filter ($ customOptionData ['values ' ], function ($ valueData ) {
217
- return empty ($ valueData ['is_delete ' ]);
218
- });
219
- }
220
-
221
- $ customOption = $ this ->getCustomOptionFactory ()->create (['data ' => $ customOptionData ]);
222
- $ customOption ->setProductSku ($ product ->getSku ());
223
- $ customOptions [] = $ customOption ;
224
- }
225
- }
226
- $ product ->setOptions ($ customOptions );
227
- }
199
+ $ product = $ this ->fillProductOptions ($ product , $ productOptions );
228
200
229
201
$ product ->setCanSaveCustomOptions (
230
202
!empty ($ productData ['affect_product_custom_options ' ]) && !$ product ->getOptionsReadonly ()
@@ -236,10 +208,10 @@ public function initializeFromData(\Magento\Catalog\Model\Product $product, arra
236
208
/**
237
209
* Initialize product before saving
238
210
*
239
- * @param \Magento\Catalog\Model\ Product $product
240
- * @return \Magento\Catalog\Model\ Product
211
+ * @param Product $product
212
+ * @return Product
241
213
*/
242
- public function initialize (\ Magento \ Catalog \ Model \ Product $ product )
214
+ public function initialize (Product $ product )
243
215
{
244
216
$ productData = $ this ->request ->getPost ('product ' , []);
245
217
return $ this ->initializeFromData ($ product , $ productData );
@@ -248,11 +220,11 @@ public function initialize(\Magento\Catalog\Model\Product $product)
248
220
/**
249
221
* Setting product links
250
222
*
251
- * @param \Magento\Catalog\Model\ Product $product
252
- * @return \Magento\Catalog\Model\ Product
223
+ * @param Product $product
224
+ * @return Product
253
225
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
254
226
*/
255
- protected function setProductLinks (\ Magento \ Catalog \ Model \ Product $ product )
227
+ protected function setProductLinks (Product $ product )
256
228
{
257
229
$ links = $ this ->getLinkResolver ()->getLinks ();
258
230
@@ -454,4 +426,51 @@ private function getDateTimeFilter()
454
426
455
427
return $ this ->dateTimeFilter ;
456
428
}
429
+
430
+ /**
431
+ * Fills $product with options from $productOptions array
432
+ *
433
+ * @param Product $product
434
+ * @param array $productOptions
435
+ * @return Product
436
+ */
437
+ private function fillProductOptions (Product $ product , array $ productOptions )
438
+ {
439
+ if ($ product ->getOptionsReadonly ()) {
440
+ return $ product ;
441
+ }
442
+ if (empty ($ productOptions )) {
443
+ return $ product ->setOptions ([]);
444
+ }
445
+ // mark custom options that should to fall back to default value
446
+ $ options = $ this ->mergeProductOptions (
447
+ $ productOptions ,
448
+ $ this ->request ->getPost ('options_use_default ' )
449
+ );
450
+ $ customOptions = [];
451
+ foreach ($ options as $ customOptionData ) {
452
+ if (!empty ($ customOptionData ['is_delete ' ])) {
453
+ continue ;
454
+ }
455
+
456
+ if (empty ($ customOptionData ['option_id ' ])) {
457
+ $ customOptionData ['option_id ' ] = null ;
458
+ }
459
+ if (isset ($ customOptionData ['values ' ])) {
460
+ $ customOptionData ['values ' ] = array_filter (
461
+ $ customOptionData ['values ' ],
462
+ function ($ valueData ) {
463
+ return empty ($ valueData ['is_delete ' ]);
464
+ }
465
+ );
466
+ }
467
+ $ customOption = $ this ->getCustomOptionFactory ()->create (
468
+ ['data ' => $ customOptionData ]
469
+ );
470
+ $ customOption ->setProductSku ($ product ->getSku ());
471
+ $ customOptions [] = $ customOption ;
472
+ }
473
+
474
+ return $ product ->setOptions ($ customOptions );
475
+ }
457
476
}
0 commit comments