This looks like a bug to me.
I noticed that the initialize
method in Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper
sets category_ids
to an empty array if category_ids
is not already present in the API request. So not providing category_ids
in the API request has the same effect as setting it to an empty array.
My workaround, until a proper fix is (hopefully) released, is to extend the Magento\Catalog\Model\ProductRepository
class and override the initializeProductData
method, and remove category_ids
altogether if it's empty. Your new ProductRepository
class then needs to be the preferred Magento\Catalog\Api\ProductRepositoryInterface
implementation.
The overriding method in my ProductRepository
class looks like this:
protected function initializeProductData(array $productData, $createNew)
{
$product = parent::initializeProductData($productData, $createNew);
$categoryIds = $product->getData('category_ids');
if ( empty($categoryIds) )
{
$product->unsetData('category_ids');
}
return $product;
}
And my di.xml has:
<preference for="Magento\Catalog\Api\ProductRepositoryInterface" type="My\Namespace\ProductRepository" />
If anyone have a nicer workaround/solution, please share.
(I have opened an issue about this, but have not received any feedback yet: https://github.com/magento/magento2/issues/3354)
category_ids
as well, when updating price, does it work?