8
8
use Magento \Catalog \Api \Data \ProductLinkInterfaceFactory ;
9
9
use Magento \Catalog \Api \ProductLinkRepositoryInterface ;
10
10
use Magento \Catalog \Model \ProductFactory ;
11
- use Magento \Catalog \Model \ProductRepository ;
12
- use Magento \Framework \Exception \NoSuchEntityException ;
13
11
use Magento \Framework \Setup \SampleData \Context as SampleDataContext ;
14
12
15
13
/**
@@ -37,11 +35,6 @@ class ProductLink
37
35
*/
38
36
private $ productLinkFactory ;
39
37
40
- /**
41
- * @var ProductRepository
42
- */
43
- private $ productRepository ;
44
-
45
38
/**
46
39
* @var ProductLinkRepositoryInterface
47
40
*/
@@ -50,23 +43,20 @@ class ProductLink
50
43
/**
51
44
* @param SampleDataContext $sampleDataContext
52
45
* @param ProductFactory $productFactory
53
- * @param ProductRepository $productRepository
54
- * @param ProductLinkRepositoryInterface $productLinkRepository
55
46
* @param ProductLinkInterfaceFactory $productLinkFactory
47
+ * @param ProductLinkRepositoryInterface $productLinkRepository
56
48
*/
57
49
public function __construct (
58
50
SampleDataContext $ sampleDataContext ,
59
51
ProductFactory $ productFactory ,
60
- ProductRepository $ productRepository ,
61
- ProductLinkRepositoryInterface $ productLinkRepository ,
62
- ProductLinkInterfaceFactory $ productLinkFactory
52
+ ProductLinkInterfaceFactory $ productLinkFactory ,
53
+ ProductLinkRepositoryInterface $ productLinkRepository
63
54
) {
64
55
$ this ->fixtureManager = $ sampleDataContext ->getFixtureManager ();
65
56
$ this ->csvReader = $ sampleDataContext ->getCsvReader ();
66
57
$ this ->productFactory = $ productFactory ;
67
- $ this ->productRepository = $ productRepository ;
68
- $ this ->productLinkRepository = $ productLinkRepository ;
69
58
$ this ->productLinkFactory = $ productLinkFactory ;
59
+ $ this ->productLinkRepository = $ productLinkRepository ;
70
60
}
71
61
72
62
/**
@@ -96,24 +86,30 @@ public function install(array $related, array $upsell, array $crosssell)
96
86
$ data [$ header [$ key ]] = $ value ;
97
87
}
98
88
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 ) {
102
92
continue ;
103
93
}
104
-
94
+ $ product ->setId ($ productId );
95
+ $ product ->setSku ($ data ['sku ' ]);
96
+ $ links = $ this ->productLinkRepository ->getList ($ product );
105
97
$ linkedProductSkus = explode ("\n" , $ data ['linked_sku ' ]);
106
98
foreach ($ linkedProductSkus as $ linkedProductSku ) {
99
+ $ linkedProduct = $ this ->productFactory ->create ();
100
+ $ linkedProductId = $ linkedProduct ->getIdBySku ($ linkedProductSku );
101
+ if (!$ linkedProductId ) {
102
+ continue ;
103
+ }
104
+
107
105
$ productLink = $ this ->productLinkFactory ->create ();
108
- $ productLink ->setSku ($ product -> getSku () )
106
+ $ productLink ->setSku ($ data [ ' sku ' ] )
109
107
->setLinkedProductSku ($ linkedProductSku )
110
108
->setLinkType ($ linkType );
111
- try {
112
- $ this ->productLinkRepository ->save ($ productLink );
113
- } catch (NoSuchEntityException $ e ) {
114
- continue ;
115
- }
109
+ $ links [] = $ productLink ;
116
110
}
111
+ $ product ->setProductLinks ($ links );
112
+ $ product ->getLinkInstance ()->saveProductRelations ($ product );
117
113
}
118
114
}
119
115
}
0 commit comments