22
22
use Magento \Catalog \Model \Product \LinkTypeProvider ;
23
23
use Magento \Framework \App \ObjectManager ;
24
24
use Magento \Framework \App \RequestInterface ;
25
+ use Magento \Framework \Exception \NoSuchEntityException ;
25
26
use Magento \Framework \Locale \FormatInterface ;
26
27
use Magento \Framework \Stdlib \DateTime \Filter \Date ;
27
28
use Magento \Store \Model \StoreManagerInterface ;
@@ -278,6 +279,7 @@ public function initialize(Product $product)
278
279
* @param Product $product
279
280
* @return Product
280
281
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
282
+ * @throws NoSuchEntityException
281
283
* @since 101.0.0
282
284
*/
283
285
protected function setProductLinks (Product $ product )
@@ -300,34 +302,21 @@ protected function setProductLinks(Product $product)
300
302
unset($ linkTypes [$ productLink ->getLinkType ()]);
301
303
}
302
304
303
- $ isReadOnlyRelatedItems = $ isReadOnlyUpSellItems = false ;
305
+ $ isReadOnlyUpSellItems = false ;
304
306
foreach ($ linkTypes as $ linkType => $ readonly ) {
305
- if ($ readonly ) {
306
- if ($ linkType === 'related ' ) {
307
- $ isReadOnlyRelatedItems = true ;
308
- } elseif ($ linkType === 'upsell ' ) {
309
- $ isReadOnlyUpSellItems = true ;
310
- }
311
- }
307
+ $ isReadOnlyRelatedItems = $ readonly && $ linkType === 'related ' ||
308
+ ($ isReadOnlyUpSellItems = $ readonly && $ linkType === 'upsell ' );
312
309
if ($ isReadOnlyRelatedItems && $ isReadOnlyUpSellItems ) {
313
310
$ productLinks = null ;
314
311
break ;
315
312
} else {
316
- if (isset ($ links [$ linkType ]) && !$ readonly ) {
317
- foreach ((array ) $ links [$ linkType ] as $ linkData ) {
318
- if (empty ($ linkData ['id ' ])) {
319
- continue ;
320
- }
321
-
322
- $ linkProduct = $ this ->productRepository ->getById ($ linkData ['id ' ]);
323
- $ link = $ this ->productLinkFactory ->create ();
324
- $ link ->setSku ($ product ->getSku ())
325
- ->setLinkedProductSku ($ linkProduct ->getSku ())
326
- ->setLinkType ($ linkType )
327
- ->setPosition (isset ($ linkData ['position ' ]) ? (int ) $ linkData ['position ' ] : 0 );
328
- $ productLinks [] = $ link ;
329
- }
330
- }
313
+ $ productLinks = $ this ->setProductLinksForNotReadOnlyItems (
314
+ $ productLinks ,
315
+ $ product ,
316
+ $ links ,
317
+ $ linkType ,
318
+ $ readonly
319
+ );
331
320
}
332
321
}
333
322
@@ -536,4 +525,39 @@ private function setCategoryLinks(Product $product): void
536
525
$ extensionAttributes ->setCategoryLinks (!empty ($ newCategoryLinks ) ? $ newCategoryLinks : null );
537
526
$ product ->setExtensionAttributes ($ extensionAttributes );
538
527
}
528
+
529
+ /**
530
+ * Set product links when readonly is false
531
+ *
532
+ * @param array $productLinks
533
+ * @param Product $product
534
+ * @param array $links
535
+ * @param string $linkType
536
+ * @param mixed $readonly
537
+ * @return array
538
+ * @throws NoSuchEntityException
539
+ */
540
+ private function setProductLinksForNotReadOnlyItems (
541
+ array $ productLinks ,
542
+ Product $ product ,
543
+ array $ links ,
544
+ string $ linkType ,
545
+ mixed $ readonly
546
+ ): array {
547
+ if (isset ($ links [$ linkType ]) && !$ readonly ) {
548
+ foreach ((array )$ links [$ linkType ] as $ linkData ) {
549
+ if (empty ($ linkData ['id ' ])) {
550
+ continue ;
551
+ }
552
+ $ linkProduct = $ this ->productRepository ->getById ($ linkData ['id ' ]);
553
+ $ link = $ this ->productLinkFactory ->create ();
554
+ $ link ->setSku ($ product ->getSku ())
555
+ ->setLinkedProductSku ($ linkProduct ->getSku ())
556
+ ->setLinkType ($ linkType )
557
+ ->setPosition (isset ($ linkData ['position ' ]) ? (int )$ linkData ['position ' ] : 0 );
558
+ $ productLinks [] = $ link ;
559
+ }
560
+ }
561
+ return $ productLinks ;
562
+ }
539
563
}
0 commit comments