Magento 2:使用REST API创建可配置产品


10

为了创建可配置产品,我需要创建可配置产品,虚拟产品,最后将它们连接起来。

json请求的示例来自以下位置:如何使用REST API v2创建可配置产品?

我想知道为什么在可配置产品中需要以下部分?

        "configurable_product_options":[
         {
           "attribute__id":"193",
           "label":"Colour",
           "position":0,
           "values":[
             {
               "value_index":340
             },
             {
               "value_index":341
             }
           ],

我注意到此部分是以后能够将虚拟产品连接到可配置连接所必需的。但是价值观没有意义。

在虚拟产品中,我可以分配所需的任何值。此值的目的是什么?

Answers:


0

请尝试以下代码,希望它对您有用。

创建了具有“颜色”属性的简单产品,简单产品ID为1011、1012和1013。

<?php
/********* Create Configurable Product By Rest API *********/
try {
    $url = "http://siteurl.com";
    $apiusername = 'apiusername';
    $apipassword = 'apipassword';
    $userData = array("username" => $apiusername, "password" => $apipassword);


    $ch = curl_init($url."/rest/V1/integration/admin/token");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($userData))));

    $token = curl_exec($ch);
    $product_data= '{
                    "product": {
                                "id": 0,
                                "sku": "config_1",
                                "name": "Config Product",
                                "attributeSetId": 4,
                                "price": 20,
                                "status": 1,
                                "visibility": 4,
                                "typeId": "configurable",
                                "createdAt": "string",
                                "updatedAt": "string",
                                "weight": 0.8,
                                "extensionAttributes": {
                                    "stockItem": {
                                        "isInStock": true
                                        },
                                    "configurableProductLinks": [1011,1012,1013],
                                    "configurableProductOptions": [
                                        {
                                            "id": 0,
                                            "attributeId": "93",
                                            "label": "Color",
                                            "position": 0,
                                            "isUseDefault": true,
                                                "values": [
                                                    {
                                                        "valueIndex": 11
                                                    },
                                                    {
                                                        "valueIndex": 12
                                                    },
                                                    {
                                                        "valueIndex": 13
                                                    }
                                                        ]
                                        }
                                                                ]   
                                   }
                            }
    }';

     $ch = curl_init($url."/rest/V1/products");
     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_POSTFIELDS,$product_data);
     curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));

     $result = curl_exec($ch);
     }catch(Exception $e){

           echo $e->getMessage();

     }
      var_dump($result);
?>  
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.