6
6
7
7
namespace Magento \Catalog \Model ;
8
8
9
- use Magento \Framework \Exception \InputException ;
9
+ use Magento \Catalog \Api \CategoryLinkRepositoryInterface ;
10
+ use Magento \Catalog \Api \CategoryListDeleteBySkuInterface ;
11
+ use Magento \Catalog \Api \CategoryRepositoryInterface ;
12
+ use Magento \Catalog \Api \ProductRepositoryInterface ;
13
+ use Magento \Catalog \Model \ResourceModel \Product ;
14
+ use Magento \Framework \App \ObjectManager ;
10
15
use Magento \Framework \Exception \CouldNotSaveException ;
16
+ use Magento \Framework \Exception \InputException ;
11
17
12
- class CategoryLinkRepository implements \Magento \Catalog \Api \CategoryLinkRepositoryInterface
18
+ /**
19
+ * @inheritdoc
20
+ */
21
+ class CategoryLinkRepository implements CategoryLinkRepositoryInterface, CategoryListDeleteBySkuInterface
13
22
{
14
23
/**
15
24
* @var CategoryRepository
16
25
*/
17
26
protected $ categoryRepository ;
18
27
19
28
/**
20
- * @var \Magento\Catalog\Api\ ProductRepositoryInterface
29
+ * @var ProductRepositoryInterface
21
30
*/
22
31
protected $ productRepository ;
23
32
24
33
/**
25
- * @param \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository
26
- * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
34
+ * @var Product
35
+ */
36
+ private $ productResource ;
37
+
38
+ /**
39
+ * @param CategoryRepositoryInterface $categoryRepository
40
+ * @param ProductRepositoryInterface $productRepository
41
+ * @param Product $productResource
27
42
*/
28
43
public function __construct (
29
- \Magento \Catalog \Api \CategoryRepositoryInterface $ categoryRepository ,
30
- \Magento \Catalog \Api \ProductRepositoryInterface $ productRepository
44
+ CategoryRepositoryInterface $ categoryRepository ,
45
+ ProductRepositoryInterface $ productRepository ,
46
+ Product $ productResource = null
31
47
) {
32
48
$ this ->categoryRepository = $ categoryRepository ;
33
49
$ this ->productRepository = $ productRepository ;
50
+ $ this ->productResource = $ productResource ?? ObjectManager::getInstance ()->get (Product::class);
34
51
}
35
52
36
53
/**
37
- * { @inheritdoc}
54
+ * @inheritdoc
38
55
*/
39
56
public function save (\Magento \Catalog \Api \Data \CategoryProductLinkInterface $ productLink )
40
57
{
@@ -60,15 +77,15 @@ public function save(\Magento\Catalog\Api\Data\CategoryProductLinkInterface $pro
60
77
}
61
78
62
79
/**
63
- * { @inheritdoc}
80
+ * @inheritdoc
64
81
*/
65
82
public function delete (\Magento \Catalog \Api \Data \CategoryProductLinkInterface $ productLink )
66
83
{
67
84
return $ this ->deleteByIds ($ productLink ->getCategoryId (), $ productLink ->getSku ());
68
85
}
69
86
70
87
/**
71
- * { @inheritdoc}
88
+ * @inheritdoc
72
89
*/
73
90
public function deleteByIds ($ categoryId , $ sku )
74
91
{
@@ -101,4 +118,44 @@ public function deleteByIds($categoryId, $sku)
101
118
}
102
119
return true ;
103
120
}
121
+
122
+ /**
123
+ * @inheritdoc
124
+ */
125
+ public function deleteBySkus (int $ categoryId , array $ productSkuList ): bool
126
+ {
127
+ $ category = $ this ->categoryRepository ->get ($ categoryId );
128
+ $ products = $ this ->productResource ->getProductsIdsBySkus ($ productSkuList );
129
+
130
+ if (!$ products ) {
131
+ throw new InputException (__ ("The category doesn't contain the specified products. " ));
132
+ }
133
+
134
+ $ productPositions = $ category ->getProductsPosition ();
135
+
136
+ foreach ($ products as $ productId ) {
137
+ if (isset ($ productPositions [$ productId ])) {
138
+ unset($ productPositions [$ productId ]);
139
+ }
140
+ }
141
+
142
+ $ category ->setPostedProducts ($ productPositions );
143
+
144
+ try {
145
+ $ category ->save ();
146
+ } catch (\Exception $ e ) {
147
+ throw new CouldNotSaveException (
148
+ __ (
149
+ 'Could not save products "%products" to category %category ' ,
150
+ [
151
+ "products " => implode (', ' , $ productSkuList ),
152
+ "category " => $ category ->getId ()
153
+ ]
154
+ ),
155
+ $ e
156
+ );
157
+ }
158
+
159
+ return true ;
160
+ }
104
161
}
0 commit comments