0

Does someone know how to update the product price with REST API without changing the category_ids attribute.

If i perform a PUT /V1/products/{sku} With body

{
 "product":
 {
 "price":"123"
 }
}

It removes the category_ids but looks like all other attributes are left unchanged.

Does anyone know a way to just update the price?

I looked through the document at http://devdocs.magento.com/swagger/ but could not find any other way to change the price on the product.

/Thomas

  • Try to pass category_ids as well, when updating price, does it work? – Alex Paliarush Dec 22 '15 at 7:43
  • it does work fine when adding category_ids. I was hoping there was a way out of the box to do this without having to know or fetch the category_ids before updating the price. – TNordkvist Dec 23 '15 at 7:39
  • My quick fix was to create an index of my own with a few REST cals but its slow. When i have time i need to find or make an api myself that can take an array of sku/price to do the price update. – TNordkvist Dec 23 '15 at 7:49
0

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)

  • My quick n'dirty was to just get the category_ids for the product before updateing the product. Works ok on few products to update. Your solution is better. I'll try it if it doesn't get solved from magento in the near future. – TNordkvist Feb 12 '16 at 7:00
0

sending "null" as value for category_ids does the trick: array("attributeCode" => "category_ids", "value" => null);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.