Skip to content

Commit d1b7097

Browse files
committed
#20481 REST products update category_ids cannot be removed
Code review, updated unit tests
1 parent b602067 commit d1b7097

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
193193
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
194194
* @param int $cacheLimit [optional]
195195
* @param ReadExtensions|null $readExtensions
196-
* @param Magento\Catalog\Api\CategoryLinkManagementInterface|null $categoryLinkManagement
196+
* @param Magento\Catalog\Api\CategoryLinkManagementInterface|null $linkManagement
197197
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
198198
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
199199
*/
@@ -638,7 +638,7 @@ public function save(ProductInterface $product, $saveOptions = false)
638638
}
639639

640640
$this->saveProduct($product);
641-
if ($assignToCategories === true) {
641+
if ($assignToCategories === true && $product->getCategoryIds()) {
642642
$this->linkManagement->assignProductToCategories(
643643
$product->getSku(),
644644
$product->getCategoryIds()

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ protected function setUp()
200200
'setData',
201201
'getStoreId',
202202
'getMediaGalleryEntries',
203-
'getExtensionAttributes'
203+
'getExtensionAttributes',
204+
'getCategoryIds'
204205
]
205206
);
206207

@@ -220,7 +221,8 @@ protected function setUp()
220221
'validate',
221222
'save',
222223
'getMediaGalleryEntries',
223-
'getExtensionAttributes'
224+
'getExtensionAttributes',
225+
'getCategoryIds'
224226
]
225227
);
226228
$this->initializedProduct->expects($this->any())
@@ -269,6 +271,12 @@ protected function setUp()
269271
$this->initializedProduct
270272
->method('getExtensionAttributes')
271273
->willReturn($this->productExtension);
274+
$this->product
275+
->method('getCategoryIds')
276+
->willReturn([1, 2, 3, 4]);
277+
$this->initializedProduct
278+
->method('getCategoryIds')
279+
->willReturn([1, 2, 3, 4]);
272280
$storeMock = $this->getMockBuilder(StoreInterface::class)
273281
->disableOriginalConstructor()
274282
->setMethods([])

0 commit comments

Comments
 (0)