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: Creating Fulfillment via API returns error

Solved

Creating Fulfillment via API returns error

ironpro
Shopify Partner
24 0 4

Hi there!
I am going to create a fulfillment using Fulfillment API but it returns error.

We are developing our own dropship management system using Shopify API with PHP package.

All the API has been successfully integrated but creating fulfillment (retrieving fulfillment orders works well) always returns error.


"Fulfillment payloads fulfillment order must be greater than 0"

Here I attach the code where I used real values for testing.

$fulfills = $this->shopify->get('/orders/5625029001545/fulfillment_orders.json');
$orderfulfills = $fulfills['fulfillment_orders'];

if (is_array($orderfulfills) && count($orderfulfills) >0) {
     foreach ($orderfulfills as $rfill) {
        if (isset($rfill['request_status']) && $rfill['request_status'] =='unsubmitted' && isset($rfill['assigned_location'])) {

            $apiKey = "***";
            $password = "***";
            $storeId = "***";
            $endpoint = "https://myshopify.com/admin/api/2023-07/fulfillments.json";

            // Prepare the data to be sent in the request body
            $data = array(
            'fulfillment' => array(
                    "fulfillment_order_id" => $rfill['id'],
                    "message"=>"Fulfilled 1 item.",
                    "notify_customer"=> false, 
                    "location_id" => $rfill['assigned_location']['location_id'],
                    "tracking_info"=> array(
                        "number" => "YMT29GRM",
                        "url" => "https://gls-group.com/DK/en/parcel-tracking",
                        "company" => "GLS"  
                    ),
                    "line_items_by_fulfillment_order"=> array(
                        array(
                            "line_item_id" => "14794802299209",
                            "fulfillable_quantity" => 1
                        )
                    )
                )    
            );

            // Convert the data to JSON format
            $jsonData = json_encode($data);

            // Set the headers for the request
            $headers = array(
            "Content-Type: application/json",
            "X-Shopify-Access-Token: ***",
            "X-Request-ID: " . uniqid(), // Generate a unique ID for the request
            );

            // Initialize cURL
            $curl = curl_init();

            // Set the cURL options
            curl_setopt($curl, CURLOPT_URL, $endpoint);
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($curl, CURLOPT_USERPWD, $apiKey . ":" . $password);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $jsonData);

            // Execute the request
            $response = curl_exec($curl);
            $responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
            echo 'Response Code: ' . $responseCode . PHP_EOL;
            echo 'Response Body: ' . $response . PHP_EOL;

            // Check for errors
            if ($response === false) {
                $error = curl_error($curl);
                echo "cURL Error: " . $error;
            } else {
                // Process the response
                $responseData = json_decode($response, true);
                // Handle the response data as needed
            }

            // Close cURL
            curl_close($curl);

        }
    }
}
Developer
Accepted Solution (1)
garyrgilbert
Shopify Partner
431 41 185

This is an accepted solution.

Hi,

 

really? I see right off the bat multiple issues.

 

First issue:

Is that you have a object instead of an array of objects for line_items_by_fulfillment_order

 

Second issue:

you have object for fulfillment_order_line_items instead of an array of objects.

 

 

"line_items_by_fulfillment_order": {

instead of

"line_items_by_fulfillment_order": [ {..},{..} ]

 

"filfillment_order_line_items": {

instead of

"filfillment_order_line_items": [ {..},{..} ]

 

Thats what it means by expected hash to be an array.

 

Cheers,

Gary

- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution

View solution in original post

Replies 14 (14)

garyrgilbert
Shopify Partner
431 41 185

Hi There,

 

Your line_items_by_fulfillment_order json is incorrect.  There is no "fulfillable_quantity" property in the line_items_by_fulfillment_order.

 

Additionally, if I am not mistaken, you do not specify the location_id when creating a fulfillment with the new api since the fulfillment_order is assigned to a specific location to begin with.

 

Cheers,

Gary

 

 

 

 

- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
ironpro
Shopify Partner
24 0 4

Hi Gary!

Thanks for your reply.

I updated the json and tried again but same error.
Also, there was location_id originally.

'fulfillment' => array(
        "location_id" => $rfill['assigned_location']['location_id'],
        "fulfillment_order_id" => $rfill['id'],
        "message"=>"The Jewllery Room fulfilled 1 item.",
        "notify_customer"=> false, 
        "tracking_info"=> array(
            "number" => "YMT29GRM",
            "url" => "https://gls-group.com/DK/en/parcel-tracking",
            "company" => "GLS"  
        ),
        "line_items_by_fulfillment_order"=> array(
            array(
                "line_item_id" => "14794802299209"
            )
        )
    )    
);


"errors":["Fulfillment payloads fulfillment order must be greater than 0"]

Developer
garyrgilbert
Shopify Partner
431 41 185

you need to have a quantity field and it needs to be greater than zero.

 

use Shopify\Rest\Admin2023_01\Fulfillment;
use Shopify\Utils;

$this->test_session = Utils::loadCurrentSession(
    $requestHeaders,
    $requestCookies,
    $isOnline
);

$fulfillment = new Fulfillment($this->test_session);
$fulfillment->message = "The package was shipped this morning.";
$fulfillment->notify_customer = false;
$fulfillment->tracking_info = [
    "number" => "1Z001985YW99744790",
    "company" => "UPS"
];
$fulfillment->line_items_by_fulfillment_order = [
    [
        "fulfillment_order_id" => 1046000793,
        "fulfillment_order_line_items" => [
            [
                "id" => 1058737520,
                "quantity" => 1
            ]
        ]
    ]
];
$fulfillment->save(
    true, // Update Object
);
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
ironpro
Shopify Partner
24 0 4

Hi Gary!

I updated the structure as follows and tested again.

$data = array(
'fulfillment' => array(
        "location_id" => $rfill['assigned_location']['location_id'],
        "message"=>"The Jewllery Room fulfilled 1 item.",
        "notify_customer"=> false, 
        "tracking_info"=> array(
            "number" => "YMT29GRM",
            "company" => "GLS"  
        ),
        "line_items_by_fulfillment_order"=> array(
            "fulfillment_order_id" => $rfill['id'],
            "filfillment_order_line_items" => array(
                "id" => "14794802299209",
                "quantity" => 1
            )
        )
    )
);


And it returns error.
"errors":{"line_items_by_fulfillment_order":"expected Hash to be a Array"

Before I saw this error and could fix the issue by updating json as the first code I attached.

Developer
garyrgilbert
Shopify Partner
431 41 185

You still have an error in your json structure. Echo it out and compare it to the structure of the json in the example below.

 

{
   "fulfillment":{
      "message":"The package was shipped this morning.",
      "notify_customer":false,
      "tracking_info":{
         "number":"1Z001985YW99744790",
         "company":"UPS"
      },
      "line_items_by_fulfillment_order":[
         {
            "fulfillment_order_id":1046000793,
            "fulfillment_order_line_items":[
               {
                  "id":1058737520,
                  "quantity":1
               }
            ]
         }
      ]
   }
}

 

- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
ironpro
Shopify Partner
24 0 4

Hi Gary!
Thank you for your help!
Yes, here's the current code again.

$data = array(
'fulfillment' => array(
        "location_id" => $rfill['assigned_location']['location_id'],
        "message"=>"The Jewllery Room fulfilled 1 item.",
        "notify_customer"=> false, 
        "tracking_info"=> array(
            "number" => "YMT29GRM",
            "company" => "GLS"  
        ),
        "line_items_by_fulfillment_order"=> array(
            "fulfillment_order_id" => $rfill['id'],
            "filfillment_order_line_items" => array(
                "id" => "14794802299209",
                "quantity" => 1
            )
        )
    )
);

// Convert the data to JSON format
$jsonData = json_encode($data);

echo $jsonData;


And the json is showing as follows when I echo.

{
  "fulfillment": {
    "location_id": 61480337591,
    "message": "The Jewllery Room fulfilled 1 item.",
    "notify_customer": false,
    "tracking_info": {
      "number": "YMT29GRM",
      "company": "GLS"
    },
    "line_items_by_fulfillment_order": {
      "fulfillment_order_id": 6661985730889,
      "filfillment_order_line_items": {
        "id": "14794802299209",
        "quantity": 1
      }
    }
  }
}


So I think there's no error in json structure.

Developer
garyrgilbert
Shopify Partner
431 41 185

This is an accepted solution.

Hi,

 

really? I see right off the bat multiple issues.

 

First issue:

Is that you have a object instead of an array of objects for line_items_by_fulfillment_order

 

Second issue:

you have object for fulfillment_order_line_items instead of an array of objects.

 

 

"line_items_by_fulfillment_order": {

instead of

"line_items_by_fulfillment_order": [ {..},{..} ]

 

"filfillment_order_line_items": {

instead of

"filfillment_order_line_items": [ {..},{..} ]

 

Thats what it means by expected hash to be an array.

 

Cheers,

Gary

- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
ironpro
Shopify Partner
24 0 4

Hi Gary!

Yes, I updated the structure and tested again.

Now it works!! Thank you so much!!

By the way, as you can see my code, I specified 1 line item id so it would fulfill only 1 item in that order. (there were 2 line items in that order).
But currently all the items in the order are fulfilled. (I attached screenshot)

Screenshot_6.png
Is it possible for me to fulfill specific item?

Developer
garyrgilbert
Shopify Partner
431 41 185

yes, if your data structure is correct it should only fulfill the line items you specify in the array of line items to fulifll. If the array is empty it will fulfill all items in the order.

 

If this is a test system you can cancel the fulfillment (which will create a new fulfillment order) which you can fulfill again and again until you get the structure and information correct.  Be advised that the id in filfillment_order_line_items is the fulfillment_order.line_items.id and not the order.line_items.id

 

Cheers

Gary

- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
ironpro
Shopify Partner
24 0 4

Okay, I am sure it will work.

Thank you so much for your kind help, Gary!

I really appreciate it!

Developer
许志鹏
New Member
9 0 0

Hi Ironpro

If the parameter format and request parameters are correct, I still cannot access them. Is it because there is still a configuration error on my end?

 

许志鹏
New Member
9 0 0

hi  Gary!

 What do I need to do when my request result returns like this. 

{
    "errors": "Not Found"
}
next my request body :
{
    "fulfillment": {
        "api_version":"2023-07",
        "message": "The package was shipped this morning.",
        "notify_customer":false,
        "line_items_by_fulfillment_order": [{
            "fulfillment_order_id": "5191216758866"
        }],
        "tracking_info": {
            "number": "9400111205548405719028",
            "company": "USPS"
        },
        "location_id":"61581918290"
    }
}
garyrgilbert
Shopify Partner
431 41 185

Hi there,

 

did you check the json structure against the documentation? For 2023-07 there is no api_version or location_id property in the json.

 

The api_version is required in the URL part not in the json body, and location is not listed in the json body either.

 

The next question is.. where did this ID come from?

 "fulfillment_order_id": "5191216758866"

 

is that the order_id "/orders/{order_id}.json" ? If so then that is your problem.

as the parameter says that should be the fulfillment_order_id, which you get by querying the fulifllment_orders endpoint.

 

admin/api/2023-07/orders/{order_id}/fulfillment_orders.json?assigned_location_id={location_id}

 

Give that a try

 

Cheers

 

- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
许志鹏
New Member
9 0 0

Thank you! Processed