Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion app/code/Magento/Catalog/Api/CategoryLinkRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ public function save(\Magento\Catalog\Api\Data\CategoryProductLinkInterface $pro
*/
public function delete(\Magento\Catalog\Api\Data\CategoryProductLinkInterface $productLink);

/**
* Remove the product assignment from the category by category id and array of sku
*
* @param int $categoryId
* @param array $sku
* @return bool will returned True if products successfully deleted
*
* @throws \Magento\Framework\Exception\CouldNotSaveException
* @throws \Magento\Framework\Exception\StateException
* @throws \Magento\Framework\Exception\InputException
*/
public function deleteByIds($categoryId, $sku);

/**
* Remove the product assignment from the category by category id and sku
*
Expand All @@ -47,5 +60,5 @@ public function delete(\Magento\Catalog\Api\Data\CategoryProductLinkInterface $p
* @throws \Magento\Framework\Exception\StateException
* @throws \Magento\Framework\Exception\InputException
*/
public function deleteByIds($categoryId, $sku);
public function deleteById($categoryId, $sku);
}
2 changes: 1 addition & 1 deletion app/code/Magento/Catalog/Model/CategoryLinkManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function assignProductToCategories($productSku, array $categoryIds)
$product = $this->getProductRepository()->get($productSku);
$assignedCategories = $this->getProductResource()->getCategoryIds($product);
foreach (array_diff($assignedCategories, $categoryIds) as $categoryId) {
$this->getCategoryLinkRepository()->deleteByIds($categoryId, $productSku);
$this->getCategoryLinkRepository()->deleteById($categoryId, $productSku);
}

foreach (array_diff($categoryIds, $assignedCategories) as $categoryId) {
Expand Down
46 changes: 44 additions & 2 deletions app/code/Magento/Catalog/Model/CategoryLinkRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,24 @@ class CategoryLinkRepository implements \Magento\Catalog\Api\CategoryLinkReposit
*/
protected $productRepository;

/**
* @var \Magento\Catalog\Model\ResourceModel\Product
*/
private $productResource;

/**
* @param \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
* @param \Magento\Catalog\Model\ResourceModel\Product $productResource
*/
public function __construct(
\Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
\Magento\Catalog\Model\ResourceModel\Product $productResource
) {
$this->categoryRepository = $categoryRepository;
$this->productRepository = $productRepository;
$this->productResource = $productResource;
}

/**
Expand Down Expand Up @@ -64,13 +72,47 @@ public function save(\Magento\Catalog\Api\Data\CategoryProductLinkInterface $pro
*/
public function delete(\Magento\Catalog\Api\Data\CategoryProductLinkInterface $productLink)
{
return $this->deleteByIds($productLink->getCategoryId(), $productLink->getSku());
return $this->deleteById($productLink->getCategoryId(), $productLink->getSku());
}

/**
* {@inheritdoc}
*/
public function deleteByIds($categoryId, $sku)
{
$category = $this->categoryRepository->get($categoryId);
$products = $this->productResource->getProductsIdsBySkus($sku);

$productPositions = $category->getProductsPosition();

foreach ($products as $productSku => $productId) {
if (isset($productPositions[$productId])) {
unset($productPositions[$productId]);
}
}

$category->setPostedProducts($productPositions);
try {
$category->save();
} catch (\Exception $e) {
throw new CouldNotSaveException(
__(
'Could not save products "%products" to category %category',
[
"products" => implode(',', $sku),
"category" => $category->getId()
]
),
$e
);
}
return true;
}

/**
* {@inheritDoc}
*/
public function deleteById($categoryId, $sku)
{
$category = $this->categoryRepository->get($categoryId);
$product = $this->productRepository->get($sku);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function testDeleteByIds()
$productMock->expects($this->once())->method('getId')->willReturn($productId);
$categoryMock->expects($this->once())->method('setPostedProducts')->with([]);
$categoryMock->expects($this->once())->method('save');
$this->assertTrue($this->model->deleteByIds($categoryId, $productSku));
$this->assertTrue($this->model->deleteById($categoryId, $productSku));
}

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

/**
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Catalog/etc/webapi.xml
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@
</resources>
</route>
<route url="/V1/categories/:categoryId/products/:sku" method="DELETE">
<service class="Magento\Catalog\Api\CategoryLinkRepositoryInterface" method="deleteByIds" />
<service class="Magento\Catalog\Api\CategoryLinkRepositoryInterface" method="deleteById" />
<resources>
<resource ref="Magento_Catalog::categories" />
</resources>
Expand Down