REST API POST / V1 / salesRules

时间:2018-01-03 15:22:47

标签: rest magento2

使用Magento REST / salesRules / API尝试创建销售规则时,我遇到了以下错误。

string(2067)“{”message“:”%fieldName是必填字段。“,”参数“:{”fieldName“:”rule“}

我的代码: -

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

请帮忙。

谢谢,

1 个答案:

答案 0 :(得分:0)

最后,能够通过salesrule API成功创建规则.... 我错过了一行代码。需要在数组'规则'。

中设置数组
    $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);

谢谢,