cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2 REST API: Change product category

Magento 2 REST API: Change product category

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!!

 

8 REPLIES

Re: Magento 2 REST API: Change product category

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 ! 

if issue solved,Click Kudos & Accept as Solution

Re: Magento 2 REST API: Change product category

Thanks @ManthanDave but it doesn't work. If I specify category in custom_attributes as you mentioned, it doesn't select any category for my product... : (((((

 

Re: Magento 2 REST API: Change product category

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 !

if issue solved,Click Kudos & Accept as Solution

Re: Magento 2 REST API: Change product category

Hi @ManthanDave,

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 : )

Re: Magento 2 REST API: Change product category

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 !

if issue solved,Click Kudos & Accept as Solution

Re: Magento 2 REST API: Change product category

Hi @ManthanDave,

 

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!

Re: Magento 2 REST API: Change product category

Hi

okay i understand !!

so previously i have 3 categories assign to this product ..

now i have added 4th categories which id is 11 ..

so here you would like to change existing category ?

meaning existing category needs to be removed and new category needs to be replaced with older one ? is it what you want ? or help me to understand more on this

if issue solved,Click Kudos & Accept as Solution

Re: Magento 2 REST API: Change product category

HI @ManthanDave,

 

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...