@@ -74,6 +74,11 @@ class Helper
74
74
* @var \Magento\Framework\Stdlib\DateTime\Filter\DateTime
75
75
*/
76
76
private $ dateTimeFilter ;
77
+
78
+ /**
79
+ * @var \Magento\Catalog\Model\Product\LinkTypeProvider
80
+ */
81
+ private $ linkTypeProvider ;
77
82
78
83
/**
79
84
* Helper constructor.
@@ -83,21 +88,25 @@ class Helper
83
88
* @param ProductLinks $productLinks
84
89
* @param \Magento\Backend\Helper\Js $jsHelper
85
90
* @param \Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter
91
+ * @param \Magento\Catalog\Model\Product\LinkTypeProvider $linkTypeProvider
86
92
*/
87
93
public function __construct (
88
94
\Magento \Framework \App \RequestInterface $ request ,
89
95
\Magento \Store \Model \StoreManagerInterface $ storeManager ,
90
96
StockDataFilter $ stockFilter ,
91
97
\Magento \Catalog \Model \Product \Initialization \Helper \ProductLinks $ productLinks ,
92
98
\Magento \Backend \Helper \Js $ jsHelper ,
93
- \Magento \Framework \Stdlib \DateTime \Filter \Date $ dateFilter
99
+ \Magento \Framework \Stdlib \DateTime \Filter \Date $ dateFilter ,
100
+ \Magento \Catalog \Model \Product \LinkTypeProvider $ linkTypeProvider = null
94
101
) {
95
102
$ this ->request = $ request ;
96
103
$ this ->storeManager = $ storeManager ;
97
104
$ this ->stockFilter = $ stockFilter ;
98
105
$ this ->productLinks = $ productLinks ;
99
106
$ this ->jsHelper = $ jsHelper ;
100
107
$ this ->dateFilter = $ dateFilter ;
108
+ $ this ->linkTypeProvider = $ linkTypeProvider ?: \Magento \Framework \App \ObjectManager::getInstance ()
109
+ ->get (\Magento \Catalog \Model \Product \LinkTypeProvider::class);
101
110
}
102
111
103
112
/**
@@ -199,14 +208,18 @@ public function initializeFromData(\Magento\Catalog\Model\Product $product, arra
199
208
$ customOptions = [];
200
209
foreach ($ options as $ customOptionData ) {
201
210
if (empty ($ customOptionData ['is_delete ' ])) {
211
+ if (empty ($ customOptionData ['option_id ' ])) {
212
+ $ customOptionData ['option_id ' ] = null ;
213
+ }
214
+
202
215
if (isset ($ customOptionData ['values ' ])) {
203
216
$ customOptionData ['values ' ] = array_filter ($ customOptionData ['values ' ], function ($ valueData ) {
204
217
return empty ($ valueData ['is_delete ' ]);
205
218
});
206
219
}
220
+
207
221
$ customOption = $ this ->getCustomOptionFactory ()->create (['data ' => $ customOptionData ]);
208
222
$ customOption ->setProductSku ($ product ->getSku ());
209
- $ customOption ->setOptionId (null );
210
223
$ customOptions [] = $ customOption ;
211
224
}
212
225
}
@@ -247,11 +260,17 @@ protected function setProductLinks(\Magento\Catalog\Model\Product $product)
247
260
248
261
$ product = $ this ->productLinks ->initializeLinks ($ product , $ links );
249
262
$ productLinks = $ product ->getProductLinks ();
250
- $ linkTypes = [
251
- 'related ' => $ product ->getRelatedReadonly (),
252
- 'upsell ' => $ product ->getUpsellReadonly (),
253
- 'crosssell ' => $ product ->getCrosssellReadonly ()
254
- ];
263
+ $ linkTypes = [];
264
+
265
+ /** @var \Magento\Catalog\Api\Data\ProductLinkTypeInterface $linkTypeObject */
266
+ foreach ($ this ->linkTypeProvider ->getItems () as $ linkTypeObject ) {
267
+ $ linkTypes [$ linkTypeObject ->getName ()] = $ product ->getData ($ linkTypeObject ->getName () . '_readonly ' );
268
+ }
269
+
270
+ // skip linkTypes that were already processed on initializeLinks plugins
271
+ foreach ($ productLinks as $ productLink ) {
272
+ unset($ linkTypes [$ productLink ->getLinkType ()]);
273
+ }
255
274
256
275
foreach ($ linkTypes as $ linkType => $ readonly ) {
257
276
if (isset ($ links [$ linkType ]) && !$ readonly ) {
@@ -315,21 +334,58 @@ public function mergeProductOptions($productOptions, $overwriteOptions)
315
334
return $ productOptions ;
316
335
}
317
336
318
- foreach ($ productOptions as $ index => $ option ) {
337
+ foreach ($ productOptions as $ optionIndex => $ option ) {
319
338
$ optionId = $ option ['option_id ' ];
339
+ $ option = $ this ->overwriteValue (
340
+ $ optionId ,
341
+ $ option ,
342
+ $ overwriteOptions
343
+ );
320
344
321
- if (!isset ($ overwriteOptions [$ optionId ])) {
322
- continue ;
345
+ if (isset ($ option ['values ' ]) && isset ($ overwriteOptions [$ optionId ]['values ' ])) {
346
+ foreach ($ option ['values ' ] as $ valueIndex => $ value ) {
347
+ if (isset ($ value ['option_type_id ' ])) {
348
+ $ valueId = $ value ['option_type_id ' ];
349
+ $ value = $ this ->overwriteValue (
350
+ $ valueId ,
351
+ $ value ,
352
+ $ overwriteOptions [$ optionId ]['values ' ]
353
+ );
354
+
355
+ $ option ['values ' ][$ valueIndex ] = $ value ;
356
+ }
357
+ }
323
358
}
324
359
360
+ $ productOptions [$ optionIndex ] = $ option ;
361
+ }
362
+
363
+ return $ productOptions ;
364
+ }
365
+
366
+ /**
367
+ * Overwrite values of fields to default, if there are option id and field name in array overwriteOptions.
368
+ *
369
+ * @param int $optionId
370
+ * @param array $option
371
+ * @param array $overwriteOptions
372
+ *
373
+ * @return array
374
+ */
375
+ private function overwriteValue ($ optionId , $ option , $ overwriteOptions )
376
+ {
377
+ if (isset ($ overwriteOptions [$ optionId ])) {
325
378
foreach ($ overwriteOptions [$ optionId ] as $ fieldName => $ overwrite ) {
326
379
if ($ overwrite && isset ($ option [$ fieldName ]) && isset ($ option ['default_ ' . $ fieldName ])) {
327
- $ productOptions [$ index ][$ fieldName ] = $ option ['default_ ' . $ fieldName ];
380
+ $ option [$ fieldName ] = $ option ['default_ ' . $ fieldName ];
381
+ if ('title ' == $ fieldName ) {
382
+ $ option ['is_delete_store_title ' ] = 1 ;
383
+ }
328
384
}
329
385
}
330
386
}
331
387
332
- return $ productOptions ;
388
+ return $ option ;
333
389
}
334
390
335
391
/**
@@ -339,8 +395,9 @@ private function getCustomOptionFactory()
339
395
{
340
396
if (null === $ this ->customOptionFactory ) {
341
397
$ this ->customOptionFactory = \Magento \Framework \App \ObjectManager::getInstance ()
342
- ->get (' Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory ' );
398
+ ->get (\ Magento \Catalog \Api \Data \ProductCustomOptionInterfaceFactory::class );
343
399
}
400
+
344
401
return $ this ->customOptionFactory ;
345
402
}
346
403
@@ -351,8 +408,9 @@ private function getProductLinkFactory()
351
408
{
352
409
if (null === $ this ->productLinkFactory ) {
353
410
$ this ->productLinkFactory = \Magento \Framework \App \ObjectManager::getInstance ()
354
- ->get (' Magento\Catalog\Api\Data\ProductLinkInterfaceFactory ' );
411
+ ->get (\ Magento \Catalog \Api \Data \ProductLinkInterfaceFactory::class );
355
412
}
413
+
356
414
return $ this ->productLinkFactory ;
357
415
}
358
416
@@ -363,8 +421,9 @@ private function getProductRepository()
363
421
{
364
422
if (null === $ this ->productRepository ) {
365
423
$ this ->productRepository = \Magento \Framework \App \ObjectManager::getInstance ()
366
- ->get (' Magento\Catalog\Api\ProductRepositoryInterface\Proxy ' );
424
+ ->get (\ Magento \Catalog \Api \ProductRepositoryInterface \Proxy::class );
367
425
}
426
+
368
427
return $ this ->productRepository ;
369
428
}
370
429
@@ -377,6 +436,7 @@ private function getLinkResolver()
377
436
if (!is_object ($ this ->linkResolver )) {
378
437
$ this ->linkResolver = ObjectManager::getInstance ()->get (LinkResolver::class);
379
438
}
439
+
380
440
return $ this ->linkResolver ;
381
441
}
382
442
@@ -391,6 +451,7 @@ private function getDateTimeFilter()
391
451
$ this ->dateTimeFilter = \Magento \Framework \App \ObjectManager::getInstance ()
392
452
->get (\Magento \Framework \Stdlib \DateTime \Filter \DateTime::class);
393
453
}
454
+
394
455
return $ this ->dateTimeFilter ;
395
456
}
396
457
}
0 commit comments