Magento 2 rest API:创建并将制造商的 select 属性选项值关联到产品

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

我使用“/all/V1/products”休息调用在 Magento 2 上创建产品。 我正在使用此代码创建如下所有设置:

     $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
                    ],
                ]
            ]
        ];

我使用此代码创建产品并将其关联到自定义制造商

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

但我收到此错误:“custom_attributes”处理期间发生错误。属性“制造商”的值无效。 “品牌名称”值的类型无效。

制造商属性已创建

Magento 中的 manufacturer 属性是 select 属性。您不能仅发送下拉值,而必须发送该值的选项 ID。

因此,使用以下负载调用端点 rest/all/V1/products 将起作用:

{
  "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"
      }
    ]
  }
}

其中 123 是选项 ID。

要获取带有制造商属性选项 ID 的选项值,您可以向端点 rest/all/V1/products/attributes/manufacturer/options 发出 GET 请求。

您可以使用以下负载向端点 rest/all/V1/products/attributes/manufacture/options 发送 POST 请求来添加 select 选项:

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