Out now! Check out the Poll results: Do you have a Shopify store?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Re: receiving error message "Add at least 1 product" when creating custom draft order

Solved

receiving error message "Add at least 1 product" when creating custom draft order

desmonic
Shopify Partner
2 0 0

I am getting error {"errors":{"base":["Add at least 1 product"]}} when trying to create a draft order using the Rest API Curl code below.  The customer id is correct.   I am not sure what I am missing here?   

 

 

 

 

$shop = "xxxxxx.myshopify.com";
$token = "xxxxxxxxxxxxxxxx";
$query = array(
    "Content-type" => "application/json"
);

$draft_param = array(
        "draft_order" =>array(
            "line-items" => array(
                "title" => "Toy Ball",
                "price" => "10.00",
                "quantity" => 1,
                "custom" => true,
                ),
            "customer" => array(
                "id" => 1655457444313,
                ),
            "use_customer_default_address" => true          
        )
    )
    ;

$draftorder = shopify_call($token, "$shop", "/admin/api/2023-07/draft_orders.json", 
$draft_param, 'POST');

 

 

 

 

Here is the functions.php file with Curl code.

 

 

 

 

 

function shopify_call($token, $shop, $api_endpoint, $query = array(), $method = 'GET', $request_headers = array()) {
    
	// Build URL
	$url = "https://" . $shop . $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]);

	}
    
}

 

 

 

 

 

 

 

Accepted Solution (1)

lizk
Shopify Staff
246 58 78

This is an accepted solution.

Hi there 👋

You will want to double check the formatting of your draft_param.

The key should be line_items not line-items.  

To learn more visit the Shopify Help Center or the Community Blog.

View solution in original post

Reply 1 (1)

lizk
Shopify Staff
246 58 78

This is an accepted solution.

Hi there 👋

You will want to double check the formatting of your draft_param.

The key should be line_items not line-items.  

To learn more visit the Shopify Help Center or the Community Blog.