Magento – Magento 2 Unable to assign Product to category via API

apimagento2

There has been similar issues posted without an answer unfortunately but I am posting two ways in the API to set a category on a product both of which give different errors.

The first is using the categories repository (catalogCategoryLinkRepositoryV1)

example
In body POST

{ 
    "productLink" :
    { 
        "sku" : "TEST_SKU5",  
        "position" : 0, 
        "categoryId" : "8",
        "extensionAttributes": {}

    }
} 

Example Response : This is the same response regardless of what category or product.

{
  "message": "Could not save product \"%1\" with position %2 to category %3",
  "parameters": [
    "144",
    0,
    "8"
  ]
}

The second is setting it under custom_attribute using the product repository in either new or update calls. Both give the same result.

When setting the custom_attribute like below regardless of what sku and category I get an error. If I leave out "category_ids" it works fine but defeats the purpose of using the API to manage product import and updates.

'custom_attributes' => array(
            array( 'attribute_code' => 'category_ids', 'value' => ["8"] ),
            array( 'attribute_code' => 'description', 'value' => 'Simple Description' ),
            array( 'attribute_code' => 'short_description', 'value' => 'Simple  Short Description' ),
            )

Error being the following, which is the only error I get regardless of what combination of sku and category even if the sku is not made yet and I am creating a new product..

"{\"message\":\"URL key for specified store already exists.\"}"

Best Answer

You have to check below code for set product in category with position,

   <?php            
        $url="http://127.0.0.1/magento2.1/"; //your custom site url...
        $token_url=$url."rest/V1/integration/admin/token";

        //Below 9 is category id....
        $product_url=$url. "rest/V1/categories/9/products"; 
        $username="admin";
        $password="admin123";
        //Authentication rest API magento2, get access token
        $ch = curl_init();
        $data = array("username" => $username, "password" => $password);
        $data_string = json_encode($data);

        $ch = curl_init($token_url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data_string))
            );
        $token = curl_exec($ch);
        $adminToken=  json_decode($token);

        //9 is category id...
        $sampleProductData = array(
                    "sku" => "24-MB01",
                    "position" => 5,
                    "category_id" => "9",
                    "extension_attributes" => array()
        );
        $categoryData = json_encode(array('productLink' => $sampleProductData));

        $setHaders = array('Content-Type:application/json','Authorization:Bearer '.$adminToken);
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL, $product_url);
        curl_setopt($ch,CURLOPT_POSTFIELDS, $categoryData);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_HTTPHEADER, $setHaders);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($ch);
        curl_close($ch);
Related Topic