Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 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(int $categoryId, array $productSkuList): bool;
}
77 changes: 67 additions & 10 deletions app/code/Magento/Catalog/Model/CategoryLinkRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,52 @@

namespace Magento\Catalog\Model;

use Magento\Framework\Exception\InputException;
use Magento\Catalog\Api\CategoryLinkRepositoryInterface;
use Magento\Catalog\Api\CategoryListDeleteBySkuInterface;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\ResourceModel\Product;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\InputException;

class CategoryLinkRepository implements \Magento\Catalog\Api\CategoryLinkRepositoryInterface
/**
* @inheritdoc
*/
class CategoryLinkRepository implements CategoryLinkRepositoryInterface, CategoryListDeleteBySkuInterface
{
/**
* @var CategoryRepository
*/
protected $categoryRepository;

/**
* @var \Magento\Catalog\Api\ProductRepositoryInterface
* @var ProductRepositoryInterface
*/
protected $productRepository;

/**
* @param \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
* @var Product
*/
private $productResource;

/**
* @param CategoryRepositoryInterface $categoryRepository
* @param ProductRepositoryInterface $productRepository
* @param Product $productResource
*/
public function __construct(
\Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository
CategoryRepositoryInterface $categoryRepository,
ProductRepositoryInterface $productRepository,
Product $productResource = null
) {
$this->categoryRepository = $categoryRepository;
$this->productRepository = $productRepository;
$this->productResource = $productResource ?? ObjectManager::getInstance()->get(Product::class);
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function save(\Magento\Catalog\Api\Data\CategoryProductLinkInterface $productLink)
{
Expand All @@ -60,15 +77,15 @@ public function save(\Magento\Catalog\Api\Data\CategoryProductLinkInterface $pro
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function delete(\Magento\Catalog\Api\Data\CategoryProductLinkInterface $productLink)
{
return $this->deleteByIds($productLink->getCategoryId(), $productLink->getSku());
}

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

/**
* @inheritdoc
*/
public function deleteBySkus(int $categoryId, array $productSkuList): bool
{
$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 $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;
}
}
Loading