Access a community of over 900,000 Shopify Merchants and Partners and engage in meaningful conversations with your peers.
I'm having trouble understanding how to make an array for the value of entitled_collection_ids. I'm using php and encoding the query string with http_build_query().
I've tried it a number of ways, but always get the error:
[response] => {"errors":{"entitled_collection_ids":"expected Hash to be a Array"}} )
PHP Code:
$MODIFY_DATA = array (
"price_rule" => array (
"title" => "Ambassador Credit",
"target_type" => "line_item",
"target_selection" => "entitled",
"entitled_collection_ids" => array(array(2723640119)),
"allocation_method" => "across",
"value_type" => "fixed_amount",
"value" => $amount * -1,
"customer_selection" => "all",
"starts_at" => "2017-01-1T17:59:10Z",
"usage_limit" => "1"
)
);
$response = shopify_call($TOKEN, $STORE_URL, "/admin/price_rules.json", $MODIFY_DATA, 'POST');
function shopify_call($token, $shop, $api_endpoint, $query = array(), $method = 'GET', $request_headers = array()) {
// Build URL
$url = "https://" . $shop . ".myshopify.com" . $api_endpoint;
if (!is_null($query) && in_array($method, array('GET', 'DELETE'))) $url = $url . "?" . http_build_query($query);
// Configure cURL
$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_SSL_VERIFYHOST, 3);
// curl_setopt($curl, CURLOPT_SSLVERSION, 3);
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, $method);
// Setup headers
$request_headers[] = "";
if (!is_null($token)) $request_headers[] = "X-Shopify-Access-Token: " . $token;
curl_setopt($curl, CURLOPT_HTTPHEADER, $request_headers);
if ($method != 'GET' && in_array($method, array('POST', 'PUT'))) {
if (is_array($query)) $query = http_build_query($query);
curl_setopt ($curl, CURLOPT_POSTFIELDS, $query);
}
// Send request to Shopify and capture any errors
$response = curl_exec($curl);
$error_number = curl_errno($curl);
$error_message = curl_error($curl);
// Close cURL to be nice
curl_close($curl);
// Return an error is cURL has a problem
if ($error_number) {
return $error_message;
} else {
// No error, return Shopify's response by parsing out the body and the headers
$response = preg_split("/\r\n\r\n|\n\n|\r\r/", $response, 2);
// Convert headers into an array
$headers = array();
$header_data = explode("\n",$response[0]);
$headers['status'] = $header_data[0]; // Does not contain a key, have to explicitly set
array_shift($header_data); // Remove status, we've already set it above
foreach($header_data as $part) {
$h = explode(":", $part);
$headers[trim($h[0])] = trim($h[1]);
}
// Return headers and Shopify's response
return array('headers' => $headers, 'response' => $response[1]);
}
}
Hey BJ.
Have you had any success changing "entitled_collection_ids" => array(array(2723640119)) to "entitled_collection_ids" => array(2723640119)?
entitled_collection_ids is meant to be a simple array of ID values rather than a simple value array containing key/value JSON objects. That might not be how PHP works but it's just a thought I thought I'd pass along.
Cheers.
Alex | Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit the Shopify Help Center or the Shopify Blog
Thanks Alex. I have tied that. I get the same error.
I've tried:
"entitled_collection_ids" => array(2723640119)
"entitled_collection_ids" => array(array(2723640119))
"entitled_collection_ids" => 2723640119
"entitled_collection_ids" => [2723640119]
"entitled_collection_ids" => array("0" => 2723640119)
"entitled_collection_ids" => array(array("0" => 2723640119))
All result in the same error...
I have the same problem.
Has anyone find a solution to this?
No solutions yet. I still haven't figured it out. I've spent so much time on it, I feel like it must be a bug... Really wish I could get it to work.
http_build_query is not actually capable of creating non-indexed arrays.
The following will fix that 🙂
preg_replace('/\%5B\d+\%5D/', '%5B%5D', http_build_query($urlParams))
Yep that was it! preg_replace fixed it.
User | RANK |
---|---|
4 | |
4 | |
4 | |
4 | |
3 |