Hi all,
I'm looking for a way to change Product Category using REST API.
I've created my product "NewShoes" sending this JSON (products/{sku} PUT with sku parameter as "NewShoes"):
{"product":{"extensionAttributes":{"category_links":[{"position":0,"category_id":"2"}]},"sku":"NewShoes","name":"New Pair of Shoes","price":10,"type_id":"simple","status":1,"attribute_set_id":4}}
I've specified product category as "2". The product creates OK.
Now I want to change product category from "2" to "3".
If I send this JSON (products/{sku} PUT with {sku} parameter as "NewShoes"):
{"product":{"extensionAttributes":{"category_links":[{"position":0,"category_id":"3"}]}}}
To Change the product category, the Magento 2 API doesn't change category to "3". What it does is put categories to ["2", "3"].
Is there anyway to change product category using REST API??
I'm using Magento 2.2.4.
Anyone can help me??
Thanks all!!
Hi @CSRABC
Well AKAIK - you need to pass this categories id into custom attributes instead of extensions attributes !
Below i am sharing the exact parameter of products :
$productData = array( 'sku' => 'TESTING' . uniqid(), 'name' => 'Simple Product ' . uniqid(), 'visibility' => 4, /*'catalog',*/ 'type_id' => 'simple', 'price' => 99.95, 'status' => 1, 'attribute_set_id' => 4, 'weight' => 1, 'custom_attributes' => array( array( 'attribute_code' => 'category_ids', 'value' => ["42","41","32"] ), array( 'attribute_code' => 'description', 'value' => 'Simple Description' ), array( 'attribute_code' => 'short_description', 'value' => 'Simple Short Description' ), ) );
I have taken this in variable so its required to convert into json format which you have already did and then try !
it will works !
Thanks @Manthan Dave but it doesn't work. If I specify category in custom_attributes as you mentioned, it doesn't select any category for my product... : (((((
Hi @CSRABC
ok - i understand !
Can you please post exact touch points and parameters you are passing ?
Like below :
URL : ???
Method name : ???
Parameters :
So its helps me to troubleshoot the issue !
Hi @Manthan Dave,
I can't post the URL because it's a local Magento installation for testing.
The metod is on catalogProductRepositoryV1:
/rest/V1/products/{sku} (PUT)
I pass as parameter the SKU and the json is the one I mentioned above.
If you pass the categories as custom_attributes the method doesn't create them...
If you pass the categories as extension_attributes the method creates them...
Thanks : )
Hi @CSRABC
ok - seems like you are correct we need to use custom_attributes for categories !
I have tried in postman app with below details and able to change categories in the products for the same !
Below is the exact details :
API URL : http://mywebsitedomainurl/index.php/rest/V1/products/24-MB01/
Method : PUT
Parameters :
{ "product":{ "extensionAttributes":{ "category_links":[ { "position":0, "category_id":"11" // here i have one category called MEN and its id is 11 - so i am assigning 11 category in this product } ] }, "sku":"24-MB01", "name":"Joust Duffle Bags", "price":10, "type_id":"simple", "status":1, "attribute_set_id":4 } }
here my product sku is : 24-MB01
previously 24-MB01 sku have only 2 categories assign - now i have use above methods and parameters and assign 3rd categories for the same.
I have tested it in postman and its working at my end
Hope it helps !
Hi @Manthan Dave,
as I understand, you previously had 2 categories assigned to the sku "24-MB01" and with this PUT have specified one category. What this PUT have done is to add 11 as third category for this SKU, isn't it??
If this is so, I think this is a bug, and can crashes several things like catalog rules (imagine you have a rule for one previous category and a rule for the new one. Which rule will Magento apply?)...
I keep trying to find a solution...
Thanks!
HI @Manthan Dave,
what I really think Magento should do es:
- Previously I have two Magento categories in a SKU. For example Categories A & B.
- With a PUT request to this SKU I specify two categories: C & D.
I think this SKU now should have categories C & D, replacing the A & B categories. What Magento does is leving the SKU with categories A, B, C & D...
I do not know if an answer is still valid due to the time it was posted.
But I had the same problem and I found your topic today, I just did a test.
In addition to putting the category in extension_attributes I also put it in custom_attributes, in the tests I performed it will always keep what you put in extension_attributes, as long as it also places in custom_attributes, I do not know if this is a bug, but it works.
PT-BR:
Não sei se uma resposta ainda é valida devido ao tempo que foi postada.
Mas tive o mesmo problema e encontrei seu tópico hoje, acabei de fazer um teste.
Além de colocar a categoria em extension_attributes também coloquei em custom_attributes, nos testes que realizei ele sempre vai manter o que você colocar em extension_attributes, desde que coloque também em custom_attributes, não sei se isso é um bug, mas funciona.
{ "product": { "sku": "00004244", "extension_attributes": { "category_links": [ { "position": 0, "category_id": "2" }, { "position": 1, "category_id": "42" }, { "position": 2, "category_id": "52" }, { "position": 3, "category_id": "56" } ] }, "custom_attributes": [ { "attribute_code": "category_ids", "value": "[]" } ] } }
In this example, the categories that I put in category_links always override, taking the category_ids from custom_attributes and then "adding" the category
PT-BR:
Nesse exemplo as categorias que eu coloco em category_links sempre sobrescrevem, tirando o category_ids do custom_attributes ele passa a "adicionar" a categoria
Sorry for the bad english