Vikas Ghai
Vikas Ghai

Reputation: 255

Error In API cURL Call: {"errors":{"price_rule":"Required parameter missing or invalid"}}

I am getting an error in cURL call for Price rule based on selected products. Following is my error:

{
   "errors":{
      "price_rule":"Required parameter missing or invalid"
   }
}

Following is my body parameters which I am sending in cURL call:

{
   "price_rule":{
      "title":"sss",
      "target_type":"line_item",
      "target_selection":"entitled",
      "allocation_method":"across",
      "value_type":"fixed_amount",
      "value":"-434",
      "customer_selection":"all",
      "prerequisite_quantity_range":{
         "greater_than_or_equal_to":"2"
      },
      "entitled_product_ids":{
         "0":"2397130326093",
         "1":"2397129965645",
         "2":"2397131898957",
         "3":"2397132324941"
      },
      "starts_at":"2019-02-22T08:08:53.000Z"
   }
}

Following is my cURL call:

$ch = curl_init();    
// curl_setopt($ch, CURLOPT_POST, count($newrule));
curl_setopt($ch, CURLOPT_URL, $access_token_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($pro_ids11));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-Shopify-Access-Token: '.$shops_token));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
if (curl_error($ch)) {
    $error_msg = curl_error($ch);
}
curl_close($ch);
if (isset($error_msg)) {
    echo $error_msg;
}

Please note that I am working in PHP. Thanks in Advance!

Upvotes: 4

Views: 2089

Answers (4)

Jigar
Jigar

Reputation: 3261

Can you change your code like this

    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_HEADER, TRUE);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($curl, CURLOPT_MAXREDIRS, 3);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($curl, CURLOPT_USERAGENT, 'My New Shopify App v.1');
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($curl, CURLOPT_TIMEOUT, 30);
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');

    // Setup headers
    $request_headers[] = "X-Shopify-Access-Token: " . $token;
    $request_headers[]='Content-Type: application/json'

    curl_setopt($curl, CURLOPT_HTTPHEADER, $request_headers);  
    $query = json_encode($query);
    curl_setopt ($curl, CURLOPT_POSTFIELDS, $query);
    $result = curl_exec($ch);

Upvotes: 7

mooga
mooga

Reputation: 3317

First : You need to send the data with 'Content-Type: application/json' and be sure of that

Second : You have to send entitled_product_ids as an array not an object

curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-Shopify-Access-Token: '.$shops_token, 
'Content-Type: application/json'));

"entitled_product_ids": [
  2397130326093,
  2397129965645,
  2397131898957,
  2397131898957
]

Upvotes: 1

Clément Baconnier
Clément Baconnier

Reputation: 6058

Your format is wrong: entitled_product_ids should be an array and not an object

"entitled_collection_ids": [
    2397130326093,
    2397129965645,
    2397131898957,
    2397132324941
],

Documentation for reference: https://help.shopify.com/en/api/reference/discounts/pricerule#create

Upvotes: 2

Marvin Klar
Marvin Klar

Reputation: 1917

entitled_product_ids should be in this format:

      "entitled_product_ids":{
         2397130326093,
         2397129965645,
         2397131898957,
         2397132324941
      },

Also, the starts_at value should be in the ISO 8601 format (Note that one 0 is missing at the end):

"starts_at":"2019-02-22T08:08:53.00Z"

Upvotes: 0

Related Questions