Skip to content

Commit 7885d47

Browse files
committed
return changes and add additional interface for CategoryLinkRepository
1 parent 98e0613 commit 7885d47

File tree

3 files changed

+68
-7
lines changed

3 files changed

+68
-7
lines changed

app/code/Magento/Catalog/Api/CategoryLinkRepositoryInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ public function save(\Magento\Catalog\Api\Data\CategoryProductLinkInterface $pro
3737
public function delete(\Magento\Catalog\Api\Data\CategoryProductLinkInterface $productLink);
3838

3939
/**
40-
* Remove the product assignment from the category by category id and array or string of sku
40+
* Remove the product assignment from the category by category id and sku
4141
*
4242
* @param int $categoryId
43-
* @param string|array $sku
43+
* @param string $sku
4444
* @return bool will returned True if products successfully deleted
4545
*
4646
* @throws \Magento\Framework\Exception\CouldNotSaveException
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Api;
8+
9+
/**
10+
* @api
11+
* @since 100.0.2
12+
*/
13+
interface CategoryListRepositoryAdditionalInterface
14+
{
15+
/**
16+
* delete by skus list
17+
*
18+
* @param int $categoryId
19+
* @param array $productSkuList
20+
* @return bool
21+
*
22+
* @throws \Magento\Framework\Exception\CouldNotSaveException
23+
* @throws \Magento\Framework\Exception\InputException
24+
*/
25+
public function deleteBySkus($categoryId, array $productSkuList);
26+
}

app/code/Magento/Catalog/Model/CategoryLinkRepository.php

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
use Magento\Framework\Exception\InputException;
1010
use Magento\Framework\Exception\CouldNotSaveException;
1111

12-
class CategoryLinkRepository implements \Magento\Catalog\Api\CategoryLinkRepositoryInterface
12+
class CategoryLinkRepository implements \Magento\Catalog\Api\CategoryLinkRepositoryInterface,
13+
\Magento\Catalog\Api\CategoryListRepositoryAdditionalInterface
1314
{
1415
/**
1516
* @var CategoryRepository
@@ -80,11 +81,43 @@ public function delete(\Magento\Catalog\Api\Data\CategoryProductLinkInterface $p
8081
*/
8182
public function deleteByIds($categoryId, $sku)
8283
{
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."));
8591
}
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+
{
86119
$category = $this->categoryRepository->get($categoryId);
87-
$products = $this->productResource->getProductsIdsBySkus($sku);
120+
$products = $this->productResource->getProductsIdsBySkus($productSkuList);
88121

89122
if (!$products) {
90123
throw new InputException(__("The category doesn't contain the specified products."));
@@ -99,20 +132,22 @@ public function deleteByIds($categoryId, $sku)
99132
}
100133

101134
$category->setPostedProducts($productPositions);
135+
102136
try {
103137
$category->save();
104138
} catch (\Exception $e) {
105139
throw new CouldNotSaveException(
106140
__(
107141
'Could not save products "%products" to category %category',
108142
[
109-
"products" => implode(',', $sku),
143+
"products" => implode(',', $productSkuList),
110144
"category" => $category->getId()
111145
]
112146
),
113147
$e
114148
);
115149
}
150+
116151
return true;
117152
}
118153
}

0 commit comments

Comments
 (0)