[英]REST API POST /V1/salesRules
I'm getting below error while trying to create sales-rule, using Magento REST/salesRules/API. 尝试使用Magento REST / salesRules / API创建销售规则时遇到错误。
string(2067) "{"message":"%fieldName is a required field.","parameters":{"fieldName":"rule"} string(2067)“ {” message“:”%fieldName是必填字段。“,” parameters“:{” fieldName“:” rule“}
My code:- 我的代码:-
$userData = array("username" => "admin", "password" => "admin123");
$ch = curl_init("http://test.local/index.php/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);
$data = [
'name' => '40% Off on Large Orders',
'store_labels'=> [],
'description' => 'Test sales rule',
'website_ids' => [1,3],
'customer_group_ids' => [0, 1, 2, 3],
"from_date" => "2018-01-03",
'uses_per_customer' => 0,
'is_active' => true,
......
......
......
];
$ch = curl_init("http://test.local/index.php/rest/V1/salesRules");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
$result = curl_exec($ch);
var_dump($result);
Please help. 请帮忙。
Thanks, 谢谢,
Finally, able to successfully create a rule via salesrule API.... I was missing one line of code. 最终,能够通过salesrule API成功创建规则。。。我缺少一行代码。 Need to set the array in array 'rule'.
需要在数组“ rule”中设置数组。
$ruleData['rule'] = $data; //important step...
$ch = curl_init("http://test.local/index.php/rest/V1/salesRules/");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($ruleData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
$result = curl_exec($ch);
var_dump($result);
Thanks, 谢谢,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.