Skip to content

Commit c5eb623

Browse files
committed
upgrade method delete by ids to inject array skus
1 parent abbfa03 commit c5eb623

File tree

5 files changed

+62
-7
lines changed

5 files changed

+62
-7
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ public function save(\Magento\Catalog\Api\Data\CategoryProductLinkInterface $pro
3636
*/
3737
public function delete(\Magento\Catalog\Api\Data\CategoryProductLinkInterface $productLink);
3838

39+
/**
40+
* Remove the product assignment from the category by category id and array of sku
41+
*
42+
* @param int $categoryId
43+
* @param array $sku
44+
* @return bool will returned True if products successfully deleted
45+
*
46+
* @throws \Magento\Framework\Exception\CouldNotSaveException
47+
* @throws \Magento\Framework\Exception\StateException
48+
* @throws \Magento\Framework\Exception\InputException
49+
*/
50+
public function deleteByIds($categoryId, $sku);
51+
3952
/**
4053
* Remove the product assignment from the category by category id and sku
4154
*
@@ -47,5 +60,5 @@ public function delete(\Magento\Catalog\Api\Data\CategoryProductLinkInterface $p
4760
* @throws \Magento\Framework\Exception\StateException
4861
* @throws \Magento\Framework\Exception\InputException
4962
*/
50-
public function deleteByIds($categoryId, $sku);
63+
public function deleteById($categoryId, $sku);
5164
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function assignProductToCategories($productSku, array $categoryIds)
9393
$product = $this->getProductRepository()->get($productSku);
9494
$assignedCategories = $this->getProductResource()->getCategoryIds($product);
9595
foreach (array_diff($assignedCategories, $categoryIds) as $categoryId) {
96-
$this->getCategoryLinkRepository()->deleteByIds($categoryId, $productSku);
96+
$this->getCategoryLinkRepository()->deleteById($categoryId, $productSku);
9797
}
9898

9999
foreach (array_diff($categoryIds, $assignedCategories) as $categoryId) {

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

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,24 @@ class CategoryLinkRepository implements \Magento\Catalog\Api\CategoryLinkReposit
2121
*/
2222
protected $productRepository;
2323

24+
/**
25+
* @var \Magento\Catalog\Model\ResourceModel\Product
26+
*/
27+
private $productResource;
28+
2429
/**
2530
* @param \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository
2631
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
32+
* @param \Magento\Catalog\Model\ResourceModel\Product $productResource
2733
*/
2834
public function __construct(
2935
\Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
30-
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository
36+
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
37+
\Magento\Catalog\Model\ResourceModel\Product $productResource
3138
) {
3239
$this->categoryRepository = $categoryRepository;
3340
$this->productRepository = $productRepository;
41+
$this->productResource = $productResource;
3442
}
3543

3644
/**
@@ -64,13 +72,47 @@ public function save(\Magento\Catalog\Api\Data\CategoryProductLinkInterface $pro
6472
*/
6573
public function delete(\Magento\Catalog\Api\Data\CategoryProductLinkInterface $productLink)
6674
{
67-
return $this->deleteByIds($productLink->getCategoryId(), $productLink->getSku());
75+
return $this->deleteById($productLink->getCategoryId(), $productLink->getSku());
6876
}
6977

7078
/**
7179
* {@inheritdoc}
7280
*/
7381
public function deleteByIds($categoryId, $sku)
82+
{
83+
$category = $this->categoryRepository->get($categoryId);
84+
$products = $this->productResource->getProductsIdsBySkus($sku);
85+
86+
$productPositions = $category->getProductsPosition();
87+
88+
foreach ($products as $productSku => $productId) {
89+
if (isset($productPositions[$productId])) {
90+
unset($productPositions[$productId]);
91+
}
92+
}
93+
94+
$category->setPostedProducts($productPositions);
95+
try {
96+
$category->save();
97+
} catch (\Exception $e) {
98+
throw new CouldNotSaveException(
99+
__(
100+
'Could not save products "%products" to category %category',
101+
[
102+
"products" => implode(',', $sku),
103+
"category" => $category->getId()
104+
]
105+
),
106+
$e
107+
);
108+
}
109+
return true;
110+
}
111+
112+
/**
113+
* {@inheritDoc}
114+
*/
115+
public function deleteById($categoryId, $sku)
74116
{
75117
$category = $this->categoryRepository->get($categoryId);
76118
$product = $this->productRepository->get($sku);

app/code/Magento/Catalog/Test/Unit/Model/CategoryLinkRepositoryTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public function testDeleteByIds()
113113
$productMock->expects($this->once())->method('getId')->willReturn($productId);
114114
$categoryMock->expects($this->once())->method('setPostedProducts')->with([]);
115115
$categoryMock->expects($this->once())->method('save');
116-
$this->assertTrue($this->model->deleteByIds($categoryId, $productSku));
116+
$this->assertTrue($this->model->deleteById($categoryId, $productSku));
117117
}
118118

119119
/**
@@ -140,7 +140,7 @@ public function testDeleteByIdsWithCouldNotSaveException()
140140
$categoryMock->expects($this->once())->method('setPostedProducts')->with([]);
141141
$categoryMock->expects($this->once())->method('getId')->willReturn($categoryId);
142142
$categoryMock->expects($this->once())->method('save')->willThrowException(new \Exception());
143-
$this->model->deleteByIds($categoryId, $productSku);
143+
$this->model->deleteById($categoryId, $productSku);
144144
}
145145

146146
/**

app/code/Magento/Catalog/etc/webapi.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@
457457
</resources>
458458
</route>
459459
<route url="/V1/categories/:categoryId/products/:sku" method="DELETE">
460-
<service class="Magento\Catalog\Api\CategoryLinkRepositoryInterface" method="deleteByIds" />
460+
<service class="Magento\Catalog\Api\CategoryLinkRepositoryInterface" method="deleteById" />
461461
<resources>
462462
<resource ref="Magento_Catalog::categories" />
463463
</resources>

0 commit comments

Comments
 (0)