9
9
use Magento \Framework \Exception \InputException ;
10
10
use Magento \Framework \Exception \CouldNotSaveException ;
11
11
12
- class CategoryLinkRepository implements \Magento \Catalog \Api \CategoryLinkRepositoryInterface
12
+ class CategoryLinkRepository implements \Magento \Catalog \Api \CategoryLinkRepositoryInterface,
13
+ \Magento \Catalog \Api \CategoryListRepositoryAdditionalInterface
13
14
{
14
15
/**
15
16
* @var CategoryRepository
@@ -80,11 +81,43 @@ public function delete(\Magento\Catalog\Api\Data\CategoryProductLinkInterface $p
80
81
*/
81
82
public function deleteByIds ($ categoryId , $ sku )
82
83
{
83
- if (!is_array ($ sku )) {
84
- $ sku = [$ sku ];
84
+ $ category = $ this ->categoryRepository ->get ($ categoryId );
85
+ $ product = $ this ->productRepository ->get ($ sku );
86
+ $ productPositions = $ category ->getProductsPosition ();
87
+
88
+ $ productID = $ product ->getId ();
89
+ if (!isset ($ productPositions [$ productID ])) {
90
+ throw new InputException (__ ("The category doesn't contain the specified product. " ));
85
91
}
92
+ $ backupPosition = $ productPositions [$ productID ];
93
+ unset($ productPositions [$ productID ]);
94
+
95
+ $ category ->setPostedProducts ($ productPositions );
96
+ try {
97
+ $ category ->save ();
98
+ } catch (\Exception $ e ) {
99
+ throw new CouldNotSaveException (
100
+ __ (
101
+ 'Could not save product "%product" with position %position to category %category ' ,
102
+ [
103
+ "product " => $ product ->getId (),
104
+ "position " => $ backupPosition ,
105
+ "category " => $ category ->getId ()
106
+ ]
107
+ ),
108
+ $ e
109
+ );
110
+ }
111
+ return true ;
112
+ }
113
+
114
+ /**
115
+ * {@inheritDoc}
116
+ */
117
+ public function deleteBySkus ($ categoryId , array $ productSkuList )
118
+ {
86
119
$ category = $ this ->categoryRepository ->get ($ categoryId );
87
- $ products = $ this ->productResource ->getProductsIdsBySkus ($ sku );
120
+ $ products = $ this ->productResource ->getProductsIdsBySkus ($ productSkuList );
88
121
89
122
if (!$ products ) {
90
123
throw new InputException (__ ("The category doesn't contain the specified products. " ));
@@ -99,20 +132,22 @@ public function deleteByIds($categoryId, $sku)
99
132
}
100
133
101
134
$ category ->setPostedProducts ($ productPositions );
135
+
102
136
try {
103
137
$ category ->save ();
104
138
} catch (\Exception $ e ) {
105
139
throw new CouldNotSaveException (
106
140
__ (
107
141
'Could not save products "%products" to category %category ' ,
108
142
[
109
- "products " => implode (', ' , $ sku ),
143
+ "products " => implode (', ' , $ productSkuList ),
110
144
"category " => $ category ->getId ()
111
145
]
112
146
),
113
147
$ e
114
148
);
115
149
}
150
+
116
151
return true ;
117
152
}
118
153
}
0 commit comments