Home > database >  Magento 2 rest API: create and associate select attribute option value for Manufacturer to product
Magento 2 rest API: create and associate select attribute option value for Manufacturer to product

Time:09-18

I use "/all/V1/products" rest call to create product on Magento 2. I'm using this code to create all settings as follow:

     $data = [
            "product" => [
                "sku" => $sku,
                "name" => $name,
                "attribute_set_id" => 4,
                "price" => $price,
                "status" => 1,
                "visibility" => 4,
                "type_id" => "simple",
                "weight" => "1",
                "extension_attributes" => [
                    "category_links" => [
                        [
                            "position" => 0,
                            "category_id" => $cat_id
                        ]
                    ],
                    "stock_item" => [
                        "qty" => $qty,
                        "is_in_stock" => true
                    ]
                ],
                "custom_attributes" => [
                    [
                        "attribute_code" => "special_price",
                        "value" => $sale_price
                    ],
                    [
                        "attribute_code" => "special_from_date",
                        "value" => "2021-02-07 00:00:00"
                    ],
                    [
                        "attribute_code" => "special_to_date",
                        "value" => "2091-02-07 00:00:00"
                    ],
                    [
                        "attribute_code" => "cost",
                        "value" => $sale_price
                    ],
                    [
                        "attribute_code" => "description",
                        "value" => $desc
                    ],
                    [
                        "attribute_code" => "short_description",
                        "value" => $short_desc
                    ],
                    [
                        "attribute_code" => "manufacturer",
                        "value" => $brand
                    ],
                ]
            ]
        ];

I use this code to create and associate product to custom manufacturer

[
  "attribute_code" => "manufacturer",
  "value" => $brand
 ],

but I get this error: Error occurred during "custom_attributes" processing. Attribute "Manufacturer" has invalid value. The "Brand Name" value's type is invalid.

The manufacturer attribute was created enter image description here

CodePudding user response:

The manufacturer attribute in Magento is a select attribute. You can't send just the dropdown value but have to send the option id of the value.

So a call to endpoint rest/all/V1/products with following payload would work:

{
  "product": {
    "sku": "sku12345678",
    "name": "Name of product",
    "attribute_set_id": 1,
    "price": 9.95,
    "status": 1,
    "visibility": 4,
    "type_id": "simple",
    "weight": "100",
    "custom_attributes": [
      {
        "attribute_code": "manufacturer",
        "value": "123"
      }
    ]
  }
}

Where 123 is the option id.

To get the option values with option ids of the manufacturer attribute, you can do a GET request to endpoint rest/all/V1/products/attributes/manufacturer/options.

You can add an select option by using a POST request to endpoint rest/all/V1/products/attributes/manufacture/options with following payload:

{
  "option": {
    "label": "ManufacturerX",
    "value": "ManufacturerX",
    "sort_order": 100
  }
}
  • Related