Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
26 changes: 26 additions & 0 deletions app/code/Magento/Catalog/Api/CategoryListDeleteBySkuInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Catalog\Api;

/**
* @api
* @since 100.0.2
*/
interface CategoryListDeleteBySkuInterface
{
/**
* delete by skus list
*
* @param int $categoryId
* @param array $productSkuList
* @return bool
*
* @throws \Magento\Framework\Exception\CouldNotSaveException
* @throws \Magento\Framework\Exception\InputException
*/
public function deleteBySkus($categoryId, array $productSkuList);
}
53 changes: 51 additions & 2 deletions app/code/Magento/Catalog/Model/CategoryLinkRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\CouldNotSaveException;

class CategoryLinkRepository implements \Magento\Catalog\Api\CategoryLinkRepositoryInterface
class CategoryLinkRepository implements \Magento\Catalog\Api\CategoryLinkRepositoryInterface,
\Magento\Catalog\Api\CategoryListDeleteBySkuInterface
{
/**
* @var CategoryRepository
Expand All @@ -21,16 +22,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 = null
) {
$this->categoryRepository = $categoryRepository;
$this->productRepository = $productRepository;
$this->productResource = $productResource ?? \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Catalog\Model\ResourceModel\Product::class);
}

/**
Expand Down Expand Up @@ -101,4 +110,44 @@ public function deleteByIds($categoryId, $sku)
}
return true;
}

/**
* {@inheritDoc}
*/
public function deleteBySkus($categoryId, array $productSkuList)
{
$category = $this->categoryRepository->get($categoryId);
$products = $this->productResource->getProductsIdsBySkus($productSkuList);

if (!$products) {
throw new InputException(__("The category doesn't contain the specified products."));
}

$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(',', $productSkuList),
"category" => $category->getId()
]
),
$e
);
}

return true;
}
}