Skip to content

Commit 9dc3a19

Browse files
committed
#20481 REST products update category_ids cannot be removed
1 parent 2f30dde commit 9dc3a19

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Catalog\Api\Data\ProductInterface;
1212
use Magento\Catalog\Model\Product\Gallery\MimeTypeExtensionMap;
1313
use Magento\Catalog\Model\ResourceModel\Product\Collection;
14+
use Magento\Catalog\Api\CategoryLinkManagementInterface;
1415
use Magento\Eav\Model\Entity\Attribute\Exception as AttributeException;
1516
use Magento\Framework\Api\Data\ImageContentInterface;
1617
use Magento\Framework\Api\Data\ImageContentInterfaceFactory;
@@ -161,6 +162,16 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
161162
*/
162163
private $readExtensions;
163164

165+
/**
166+
* @var CategoryLinkManagementInterface
167+
*/
168+
private $categoryLinkManagement;
169+
170+
/**
171+
* @var AssignProductToCategories
172+
*/
173+
private $assignProductToCategories = false;
174+
164175
/**
165176
* ProductRepository constructor.
166177
* @param ProductFactory $productFactory
@@ -187,6 +198,7 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
187198
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
188199
* @param int $cacheLimit [optional]
189200
* @param ReadExtensions|null $readExtensions
201+
* @param Magento\Catalog\Api\CategoryLinkManagementInterface|null $categoryLinkManagement
190202
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
191203
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
192204
*/
@@ -214,7 +226,8 @@ public function __construct(
214226
CollectionProcessorInterface $collectionProcessor = null,
215227
\Magento\Framework\Serialize\Serializer\Json $serializer = null,
216228
$cacheLimit = 1000,
217-
ReadExtensions $readExtensions = null
229+
ReadExtensions $readExtensions = null,
230+
CategoryLinkManagementInterface $categoryLinkManagement = null
218231
) {
219232
$this->productFactory = $productFactory;
220233
$this->collectionFactory = $collectionFactory;
@@ -239,6 +252,8 @@ public function __construct(
239252
$this->cacheLimit = (int)$cacheLimit;
240253
$this->readExtensions = $readExtensions ?: \Magento\Framework\App\ObjectManager::getInstance()
241254
->get(ReadExtensions::class);
255+
$this->categoryLinkManagement = $categoryLinkManagement ?:\Magento\Framework\App\ObjectManager::getInstance()
256+
->get(CategoryLinkManagementInterface::class);
242257
}
243258

244259
/**
@@ -589,6 +604,7 @@ public function save(ProductInterface $product, $saveOptions = false)
589604
$extensionAttributes = $product->getExtensionAttributes();
590605
if (empty($extensionAttributes->__toArray())) {
591606
$product->setExtensionAttributes($existingProduct->getExtensionAttributes());
607+
$this->assignProductToCategories = true;
592608
}
593609
} catch (NoSuchEntityException $e) {
594610
$existingProduct = null;
@@ -626,6 +642,12 @@ public function save(ProductInterface $product, $saveOptions = false)
626642
}
627643

628644
$this->saveProduct($product);
645+
if ($this->assignProductToCategories === true) {
646+
$this->categoryLinkManagement->assignProductToCategories(
647+
$product->getSku(),
648+
$product->getCategoryIds()
649+
);
650+
}
629651
$this->removeProductFromLocalCache($product->getSku());
630652
unset($this->instancesById[$product->getId()]);
631653

0 commit comments

Comments
 (0)