2222use Magento \Catalog \Model \Product \LinkTypeProvider ;
2323use Magento \Framework \App \ObjectManager ;
2424use Magento \Framework \App \RequestInterface ;
25+ use Magento \Framework \Exception \NoSuchEntityException ;
2526use Magento \Framework \Locale \FormatInterface ;
2627use Magento \Framework \Stdlib \DateTime \Filter \Date ;
2728use Magento \Store \Model \StoreManagerInterface ;
@@ -278,6 +279,7 @@ public function initialize(Product $product)
278279 * @param Product $product
279280 * @return Product
280281 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
282+ * @throws NoSuchEntityException
281283 * @since 101.0.0
282284 */
283285 protected function setProductLinks (Product $ product )
@@ -300,34 +302,21 @@ protected function setProductLinks(Product $product)
300302 unset($ linkTypes [$ productLink ->getLinkType ()]);
301303 }
302304
303- $ isReadOnlyRelatedItems = $ isReadOnlyUpSellItems = false ;
305+ $ isReadOnlyUpSellItems = false ;
304306 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 ' );
312309 if ($ isReadOnlyRelatedItems && $ isReadOnlyUpSellItems ) {
313310 $ productLinks = null ;
314311 break ;
315312 } 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+ );
331320 }
332321 }
333322
@@ -536,4 +525,39 @@ private function setCategoryLinks(Product $product): void
536525 $ extensionAttributes ->setCategoryLinks (!empty ($ newCategoryLinks ) ? $ newCategoryLinks : null );
537526 $ product ->setExtensionAttributes ($ extensionAttributes );
538527 }
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+ }
539563}
0 commit comments