我想在postman客户端中通过REST API创建一个捆绑产品,但我似乎找不到合适的API来创建相同的API。我可以通过REST API添加捆绑选项,但要添加这些选项,我需要一个捆绑产品。所以有人可以帮我解决这个问题吗?
答案 0 :(得分:0)
我在尝试使用Magento 2 API创建捆绑产品时遇到了一些问题,但最终弄明白了,因为每次我用Google搜索时都会出现此帖子,我将在此处回答您需要通过邮递员提出的要求才能创建捆绑产品。将url http:// {{host}} / rest / default / V1 / products与POST方法一起使用,并将Authorization标头与从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
}
确保要添加到“ product_links”中的产品存在,否则将不会创建捆绑销售产品。注意“ bundle_product_options”,它必须是一个选项数组,我的错误是我试图创建一个选项并且没有在数组中传递它。
答案 1 :(得分:0)
要通过REST API在Magento 2中创建捆绑产品:
//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环境中退出。
编码愉快!干杯!
答案 2 :(得分:-1)
要通过REST API在Magento 2中创建捆绑产品,我们将按照以下步骤进行操作:
第1步 - 首先让我们编写配置(URL&#39; s,用户名,密码)
//配置数据
ResourceManager
第2步 - 然后让我们获取访问令牌
//验证rest API magento2,获取访问令牌
$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)
);
第3步 - 不让我们准备产品数据
//创建可配置产品
$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);
第4步 - 最后,我们会将产品数据发送给magento以创建产品
$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));