Magento2 如何通过RESTAPI在Magento 2中创建捆绑产品?

Magento2 如何通过RESTAPI在Magento 2中创建捆绑产品?,magento2,Magento2,我想通过postman客户端中的RESTAPI创建捆绑产品,但似乎找不到正确的API来创建捆绑产品。我可以通过REST API添加捆绑包选项,但要添加这些选项,我需要一个捆绑包产品。那么有人能帮我解决这个问题吗?要通过REST API在Magento 2中创建捆绑包产品,我们将遵循以下步骤: 步骤1-首先让我们写下配置URL、用户名和密码 //配置数据 $url="http://www.example.com/index.php/"; $token_url=$url."rest/V1/integ

我想通过postman客户端中的RESTAPI创建捆绑产品,但似乎找不到正确的API来创建捆绑产品。我可以通过REST API添加捆绑包选项,但要添加这些选项,我需要一个捆绑包产品。那么有人能帮我解决这个问题吗?

要通过REST API在Magento 2中创建捆绑包产品,我们将遵循以下步骤:

步骤1-首先让我们写下配置URL、用户名和密码

//配置数据

$url="http://www.example.com/index.php/";
$token_url=$url."rest/V1/integration/admin/token";
$product_url=$url. "rest/V1/products";
$username="your admin username";
$password="your admin password";

$product_links = array(
                        array("sku"=>"cpu1","qty"=>1)
                    );
第2步-然后让我们获取访问令牌

$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);
//身份验证rest API magento2,获取访问令牌

$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);
第3步-不,让我们准备产品数据

//创建可配置的产品

$configProductData = array(

        'sku'               => 'bundle'. uniqid(),
        'name'              => 'Bundle' . uniqid(),
        'visibility'        => 4, /*'catalog',*/
        'type_id'           => 'bundle',
        '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' => 'Description' ),
                array( 'attribute_code' => 'short_description', 'value' => 'Short Description' ),
                array( 'attribute_code' => 'price_view', 'value' => '0' ),
                array( 'attribute_code' => 'price_type', 'value' => '0' ),
        ),
        'extension_attributes' => array("bundle_product_options"=>array(
            array("title"=>"CPU","type"=>"select","product_links"=>$product_links),
        ),
        )
);
$productData = json_encode(array('product' => $configProductData));
步骤4-最后,我们将向magento发送产品数据以创建产品

$setHaders = array('Content-Type:application/json','Authorization:Bearer '.$adminToken);

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $product_url);
curl_setopt($ch,CURLOPT_POSTFIELDS, $productData);
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);
var_dump($response);

我在尝试用Magento 2 API创建捆绑产品时遇到了一些问题,但最终还是解决了,因为每次我在谷歌上搜索时都会出现这篇文章,我会在这里回答你的请求,你需要通过邮递员来创建捆绑产品。使用url http://{{host}}/rest/default/V1/products with method POST和带有从http://{{{host}}/rest/default/V1/integration/admin/token接收到的令牌的授权头,您必须将以下主体作为JSON发送,以便创建包含相关产品的捆绑产品:

{
  "product": {
    "sku": "some-bundle-sku",
    "name": null,
    "attribute_set_id": 4,
    "price": 1000,
    "status": 1,
    "visibility": 1,
    "type_id": "bundle",
    "created_at": "2018-01-01 00:00:00",
    "updated_at": "2018-01-01 00:00:00",
    "extension_attributes": {
      "stock_item": {
        "qty": 100
      },
      "website_ids": [
        1
      ],
      "category_links": [],
      "bundle_product_options": [
        {
          "option_id": 0,
          "position": 1,
          "sku": "bundle-option-sku",
          "title": "Bundle products",
          "type": "select",
          "required": true,
          "product_links": [
            {
              "sku": "some prod 1 sku",
              "option_id": 1,
              "qty": 1,
              "position": 1,
              "is_default": false,
              "price": null,
              "price_type": null,
              "can_change_quantity": 0
            },
            {
              "sku": "some prod 2 sku",
              "option_id": 1,
              "qty": 1,
              "position": 2,
              "is_default": false,
              "price": null,
              "price_type": null,
              "can_change_quantity": 0
            }
          ]
        }
      ]
    },
    "custom_attributes": [
      {
        "attribute_code": "price_view",
        "value": "0"
      }
    ]
  },
  "saveOptions": true
}

确保您在产品链接中添加的产品存在,否则它将不会创建捆绑产品。注意bundle\u product\u选项,这需要一个选项数组,我的错误是我试图创建一个选项,但没有在数组中传递它。

要通过REST API在Magento 2中创建bundle产品:

//API URL for authentication

$apiURL="http://{host}/rest/V1/integration/admin/token";

//parameters passing with URL

$data = array("username" => "<username>", "password" => "<password>");  

$data_string = json_encode($data);

$ch = curl_init($apiURL);

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

//decoding generated token and saving it in a variable

$token =  json_decode($token);

print_r($token);

//Using above token into header

$headers = array("Content-Type: application/json", "Authorization: Bearer ".$token);

//API URL to Add Product(s) in Magento

$requestUrl='http://{host}/rest/default/V1/products';

$toBeAdded = '{
  "product": {
    "sku": "some-bundle-sku",
    "name": Demo-Bundle,
    "attribute_set_id": 4,
    "price": 1000,
    "status": 1,
    "visibility": 1,
    "type_id": "bundle",
    "created_at": "2019-04-28 00:00:00",
    "updated_at": "2019-04-28 00:00:00",
    "extension_attributes": {
      "stock_item": {
        "qty": 100
      },
      "website_ids": [
        1
      ],
      "category_links": [],
      "bundle_product_options": [
        {
          "option_id": 0,
          "position": 1,
          "sku": "bundle-option-sku",
          "title": "Bundle products",
          "type": "select",
          "required": true,
          "product_links": [
            {
              "sku": "Sample",
              "option_id": 1,
              "qty": 1,
              "position": 1,
              "is_default": false,
              "price": 20,
              "price_type": null,
              "can_change_quantity": 0
            },
            {
              "sku": "Demo",
              "option_id": 1,
              "qty": 1,
              "position": 2,
              "is_default": false,
              "price": 30,
              "price_type": null,
              "can_change_quantity": 0
            }
          ]
        }
      ]
    },
    "custom_attributes": [
      {
        "attribute_code": "price_view",
        "value": "0"
      }
    ]
  },
  "saveOptions": true
}';

$ch = curl_init();
$ch = curl_init($requestUrl); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   
curl_setopt($ch, CURLOPT_POST, true);

$options = array(
    CURLOPT_POSTFIELDS=>$toBeAdded
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
$result=  json_decode($result);

echo "<pre>";
print_r($result);
这将解决您的问题。还要确保分配的用户有权访问所需的资源,并且您作为捆绑选项添加的产品必须退出您的Magento环境

快乐编码!干杯