7
7
namespace Magento \Catalog \Model \ProductLink ;
8
8
9
9
use Magento \Catalog \Api \Data ;
10
- use Magento \Catalog \Model \Product \Initialization \Helper \ProductLinks as LinksInitializer ;
11
10
use Magento \Framework \Exception \CouldNotSaveException ;
12
11
use Magento \Framework \Exception \NoSuchEntityException ;
13
12
@@ -18,52 +17,20 @@ class Management implements \Magento\Catalog\Api\ProductLinkManagementInterface
18
17
*/
19
18
protected $ productRepository ;
20
19
21
- /**
22
- * @var CollectionProvider
23
- */
24
- protected $ entityCollectionProvider ;
25
-
26
- /**
27
- * @var LinksInitializer
28
- */
29
- protected $ linkInitializer ;
30
-
31
- /**
32
- * @var \Magento\Catalog\Api\Data\ProductLinkInterfaceFactory
33
- */
34
- protected $ productLinkFactory ;
35
-
36
- /**
37
- * @var \Magento\Catalog\Model\Resource\Product
38
- */
39
- protected $ productResource ;
40
-
41
20
/**
42
21
* @var \Magento\Catalog\Model\Product\LinkTypeProvider
43
22
*/
44
23
protected $ linkTypeProvider ;
45
24
46
25
/**
47
26
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
48
- * @param CollectionProvider $collectionProvider
49
- * @param \Magento\Catalog\Api\Data\ProductLinkInterfaceFactory $productLinkFactory
50
- * @param LinksInitializer $linkInitializer
51
- * @param \Magento\Catalog\Model\Resource\Product $productResource
52
27
* @param \Magento\Catalog\Model\Product\LinkTypeProvider $linkTypeProvider
53
28
*/
54
29
public function __construct (
55
30
\Magento \Catalog \Api \ProductRepositoryInterface $ productRepository ,
56
- CollectionProvider $ collectionProvider ,
57
- \Magento \Catalog \Api \Data \ProductLinkInterfaceFactory $ productLinkFactory ,
58
- LinksInitializer $ linkInitializer ,
59
- \Magento \Catalog \Model \Resource \Product $ productResource ,
60
31
\Magento \Catalog \Model \Product \LinkTypeProvider $ linkTypeProvider
61
32
) {
62
33
$ this ->productRepository = $ productRepository ;
63
- $ this ->entityCollectionProvider = $ collectionProvider ;
64
- $ this ->productLinkFactory = $ productLinkFactory ;
65
- $ this ->productResource = $ productResource ;
66
- $ this ->linkInitializer = $ linkInitializer ;
67
34
$ this ->linkTypeProvider = $ linkTypeProvider ;
68
35
}
69
36
@@ -73,27 +40,22 @@ public function __construct(
73
40
public function getLinkedItemsByType ($ sku , $ type )
74
41
{
75
42
$ output = [];
76
- $ product = $ this -> productRepository -> get ( $ sku );
77
- try {
78
- $ collection = $ this -> entityCollectionProvider -> getCollection ( $ product , $ type );
79
- } catch ( NoSuchEntityException $ e ) {
43
+
44
+ $ linkTypes = $ this -> linkTypeProvider -> getLinkTypes ();
45
+
46
+ if (! isset ( $ linkTypes [ $ type ]) ) {
80
47
throw new NoSuchEntityException (__ ('Unknown link type: %1 ' , (string )$ type ));
81
48
}
82
- foreach ($ collection as $ item ) {
83
- /** @var \Magento\Catalog\Api\Data\ProductLinkInterface $productLink */
84
- $ productLink = $ this ->productLinkFactory ->create ();
85
- $ productLink ->setProductSku ($ product ->getSku ())
86
- ->setLinkType ($ type )
87
- ->setLinkedProductSku ($ item ['sku ' ])
88
- ->setLinkedProductType ($ item ['type ' ])
89
- ->setPosition ($ item ['position ' ]);
90
- if (isset ($ item ['custom_attributes ' ])) {
91
- foreach ($ item ['custom_attributes ' ] as $ option ) {
92
- $ productLink ->getExtensionAttributes ()->setQty ($ option ['value ' ]);
93
- }
49
+ $ product = $ this ->productRepository ->get ($ sku );
50
+ $ links = $ product ->getProductLinks ();
51
+
52
+ // Only return the links of type specified
53
+ foreach ($ links as $ link ) {
54
+ if ($ link ->getLinkType () == $ type ) {
55
+ $ output [] = $ link ;
94
56
}
95
- $ output [] = $ productLink ;
96
57
}
58
+
97
59
return $ output ;
98
60
}
99
61
@@ -111,32 +73,27 @@ public function setProductLinks($sku, $type, array $items)
111
73
}
112
74
113
75
$ product = $ this ->productRepository ->get ($ sku );
114
- $ assignedSkuList = [];
115
- /** @var \Magento\Catalog\Api\Data\ProductLinkInterface $link */
116
- foreach ($ items as $ link ) {
117
- $ assignedSkuList [] = $ link ->getLinkedProductSku ();
118
- }
119
- $ linkedProductIds = $ this ->productResource ->getProductsIdsBySkus ($ assignedSkuList );
120
76
121
- $ links = [];
122
- /** @var \Magento\Catalog\Api\Data\ProductLinkInterface[] $items*/
123
- foreach ($ items as $ link ) {
124
- $ data = $ link ->__toArray ();
125
- $ linkedSku = $ link ->getLinkedProductSku ();
126
- if (!isset ($ linkedProductIds [$ linkedSku ])) {
127
- throw new NoSuchEntityException (
128
- __ ('Product with SKU "%1" does not exist ' , $ linkedSku )
129
- );
77
+ // Replace only links of the specified type
78
+ $ existingLinks = $ product ->getProductLinks ();
79
+ $ newLinks = [];
80
+ if (!empty ($ existingLinks )) {
81
+ foreach ($ existingLinks as $ link ) {
82
+ if ($ link ->getLinkType () != $ type ) {
83
+ $ newLinks [] = $ link ;
84
+ }
130
85
}
131
- $ data ['product_id ' ] = $ linkedProductIds [$ linkedSku ];
132
- $ links [$ linkedProductIds [$ linkedSku ]] = $ data ;
86
+ $ newLinks = array_merge ($ newLinks , $ items );
87
+ } else {
88
+ $ newLinks = $ items ;
133
89
}
134
- $ this -> linkInitializer -> initializeLinks ( $ product , [ $ type => $ links ] );
90
+ $ product -> setProductLinks ( $ newLinks );
135
91
try {
136
- $ product -> save ();
92
+ $ this -> productRepository -> save ($ product );
137
93
} catch (\Exception $ exception ) {
138
94
throw new CouldNotSaveException (__ ('Invalid data provided for linked products ' ));
139
95
}
96
+
140
97
return true ;
141
98
}
142
99
}
0 commit comments