88use Magento \Catalog \Api \Data \ProductLinkInterfaceFactory ;
99use Magento \Catalog \Api \ProductLinkRepositoryInterface ;
1010use Magento \Catalog \Model \ProductFactory ;
11- use Magento \Catalog \Model \ProductRepository ;
12- use Magento \Framework \Exception \NoSuchEntityException ;
1311use Magento \Framework \Setup \SampleData \Context as SampleDataContext ;
1412
1513/**
@@ -37,11 +35,6 @@ class ProductLink
3735 */
3836 private $ productLinkFactory ;
3937
40- /**
41- * @var ProductRepository
42- */
43- private $ productRepository ;
44-
4538 /**
4639 * @var ProductLinkRepositoryInterface
4740 */
@@ -50,23 +43,20 @@ class ProductLink
5043 /**
5144 * @param SampleDataContext $sampleDataContext
5245 * @param ProductFactory $productFactory
53- * @param ProductRepository $productRepository
54- * @param ProductLinkRepositoryInterface $productLinkRepository
5546 * @param ProductLinkInterfaceFactory $productLinkFactory
47+ * @param ProductLinkRepositoryInterface $productLinkRepository
5648 */
5749 public function __construct (
5850 SampleDataContext $ sampleDataContext ,
5951 ProductFactory $ productFactory ,
60- ProductRepository $ productRepository ,
61- ProductLinkRepositoryInterface $ productLinkRepository ,
62- ProductLinkInterfaceFactory $ productLinkFactory
52+ ProductLinkInterfaceFactory $ productLinkFactory ,
53+ ProductLinkRepositoryInterface $ productLinkRepository
6354 ) {
6455 $ this ->fixtureManager = $ sampleDataContext ->getFixtureManager ();
6556 $ this ->csvReader = $ sampleDataContext ->getCsvReader ();
6657 $ this ->productFactory = $ productFactory ;
67- $ this ->productRepository = $ productRepository ;
68- $ this ->productLinkRepository = $ productLinkRepository ;
6958 $ this ->productLinkFactory = $ productLinkFactory ;
59+ $ this ->productLinkRepository = $ productLinkRepository ;
7060 }
7161
7262 /**
@@ -96,24 +86,30 @@ public function install(array $related, array $upsell, array $crosssell)
9686 $ data [$ header [$ key ]] = $ value ;
9787 }
9888
99- try {
100- $ product = $ this -> productRepository -> get ($ data ['sku ' ]);
101- } catch ( NoSuchEntityException $ e ) {
89+ $ product = $ this -> productFactory -> create ();
90+ $ productId = $ product -> getIdBySku ($ data ['sku ' ]);
91+ if (! $ productId ) {
10292 continue ;
10393 }
104-
94+ $ product ->setId ($ productId );
95+ $ product ->setSku ($ data ['sku ' ]);
96+ $ links = $ this ->productLinkRepository ->getList ($ product );
10597 $ linkedProductSkus = explode ("\n" , $ data ['linked_sku ' ]);
10698 foreach ($ linkedProductSkus as $ linkedProductSku ) {
99+ $ linkedProduct = $ this ->productFactory ->create ();
100+ $ linkedProductId = $ linkedProduct ->getIdBySku ($ linkedProductSku );
101+ if (!$ linkedProductId ) {
102+ continue ;
103+ }
104+
107105 $ productLink = $ this ->productLinkFactory ->create ();
108- $ productLink ->setSku ($ product -> getSku () )
106+ $ productLink ->setSku ($ data [ ' sku ' ] )
109107 ->setLinkedProductSku ($ linkedProductSku )
110108 ->setLinkType ($ linkType );
111- try {
112- $ this ->productLinkRepository ->save ($ productLink );
113- } catch (NoSuchEntityException $ e ) {
114- continue ;
115- }
109+ $ links [] = $ productLink ;
116110 }
111+ $ product ->setProductLinks ($ links );
112+ $ product ->getLinkInstance ()->saveProductRelations ($ product );
117113 }
118114 }
119115 }
0 commit comments