简体   繁体   English

REST API POST / V1 / salesRules

[英]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.

相关问题 Node js rest API返回错误,提示“无法发布/ api / v1 / user” - Node js rest API returning an error says 'Cannot POST /api/v1/user' Magento 2 REST-API POST V1/guest-carts/{cartId}/items 400 或 404 - Magento 2 REST-API POST V1/guest-carts/{cartId}/items either 400 or 404 Google CSE REST API:v1已被弃用,是否有v2? - Google CSE REST API : is v1 deprecated, is there a v2? 谷歌云数据存储 REST API v1 (gqlQuery) - Google Cloud Datastore REST API v1 (gqlQuery) 用于获取令牌的Openshift REST API不会将其保存到oapi / v1 / oauthaccesstokens中 - Openshift REST API for getting token is not save it into oapi/v1/oauthaccesstokens V1 REST API查询以查询团队和成员名称 - V1 REST API queries for lsiting the Team & Member Name Magento 2 Rest Api V1 /产品缺少扩展属性 - Magento 2 Rest Api V1/products missing extension attributes 可以使用LinkedIn v1 API(春季社交网络)回复帖子评论吗? - Is it possible reply post comments with LinkedIn v1 API (spring social)? LinkedIn Ads Rest API-我可以连接到/ v1 / people /〜,但不能连接到/ v2 / adAnalyticsV2? - LinkedIn Ads Rest API - I can connect to /v1/people/~ but not the /v2/adAnalyticsV2? 如何在Prometheus 2.11 / api / v1 / alerts rest api调用中过滤警报结果 - How to filter alerts results in prometheus 2.11 /api/v1/alerts rest api call
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM