Solved

Fulfilment api not working

devserge
Shopify Partner
4 2 4

Hey all,

 

I am currently trying to work with the fulfilment api because of an third party integration. This is my code;

 

 

 

 

        // Set the Shopify store URL and access token
        $storeUrl = '###;
        $accessToken = '###';

        // Set the order name to search for
        $orderName = $order;

        // Build the API endpoint URL to retrieve the order by its name
        $endpoint = $storeUrl . '/admin/api/2023-04/orders.json?name=' . $orderName;

        // Set the CURL options to make the API request
        $options = array(
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_URL => $endpoint,
            CURLOPT_HTTPHEADER => array(
                'Content-Type: application/json',
                'X-Shopify-Access-Token: ' . $accessToken
            )
        );

        // Create a CURL session and make the API request
        $curl = curl_init();
        curl_setopt_array($curl, $options);
        $response = curl_exec($curl);

        // Parse the API response and get the order ID
        $order = json_decode($response, true)['orders'][0];
        $orderId = $order['id'];

        // Build the API endpoint URL to create a fulfillment for the order
        $endpoint = $storeUrl . '/admin/api/2023-04/orders/' . $orderId . '/fulfillments.json';

        // Build the fulfillment data to send in the API request
        $fulfillmentData = array(
            'fulfillment' => array(
                'location_id' => 81145069881, // replace with your location ID
                'tracking_number' => $tracenumber,
                'tracking_company' => 'DHL - Parcel',
                'line_items' => array(),
                'notify_customer' => true,
                'message' => 'Your order has been send!'
            )
        );

        // Get all the line items for the order and add them to the fulfillment data
        $lineItems = $order['line_items'];
        foreach ($lineItems as $lineItem) {
            $fulfillmentData['fulfillment']['line_items'][] = array(
                'id' => $lineItem['id'],
                'quantity' => $lineItem['quantity']
            );
        }

        // Set the CURL options to make the API request to create the fulfillment
        $options = array(
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POST => true,
            CURLOPT_URL => $endpoint,
            CURLOPT_HTTPHEADER => array(
                'Content-Type: application/json',
                'X-Shopify-Access-Token: ' . $accessToken
            ),
            CURLOPT_POSTFIELDS => json_encode($fulfillmentData)
        );

        // Make the API request to create the fulfillment
        curl_setopt_array($curl, $options);
        $response = curl_exec($curl);
        dd($response);

        // Close the CURL session
        curl_close($curl);

        // Parse the API response and check for any errors
        $responseData = json_decode($response, true);
        if (isset($responseData['errors'])) {
            echo 'Error: ' . $responseData['errors'];
        } else {
            echo 'Fulfillment created for order ' . $orderId;
        }


    }

 

 

 

 

Fetching the order is working fine, but creating the fulfilment is not. The response is nothing, not an error, no success. Like never happens..

 

Does anyone have the experience with the fulfilment API? Am i missing something?

 

Please let me know,

 

Kind regards,

Serge Huijsen

Accepted Solution (1)

devserge
Shopify Partner
4 2 4

This is an accepted solution.

It was caused by giving the wrong information to the endpoint. I now use this code, that is working:

 

        $data = array(
            'fulfillment' => array(
                'message' => 'The order has been send',
                'notify_customer' => true,
                'location_id' => 435435435,
                'tracking_info' => array(
                    'number' => 5858371,
                    'url' => 'dhl.com',
                    'company' => 'DHL - Parcel'
                ),
                'line_items_by_fulfillment_order' => array(
                    array(
                        'fulfillment_order_id' => 8757845345,
                    )
                )
            )
        );

View solution in original post

Replies 2 (2)

brentonMN
Shopify Partner
10 0 5

I'm having a similar issue. Returning for me the error below:

{ errors: { fulfillment: 'Required parameter missing or invalid' } }

devserge
Shopify Partner
4 2 4

This is an accepted solution.

It was caused by giving the wrong information to the endpoint. I now use this code, that is working:

 

        $data = array(
            'fulfillment' => array(
                'message' => 'The order has been send',
                'notify_customer' => true,
                'location_id' => 435435435,
                'tracking_info' => array(
                    'number' => 5858371,
                    'url' => 'dhl.com',
                    'company' => 'DHL - Parcel'
                ),
                'line_items_by_fulfillment_order' => array(
                    array(
                        'fulfillment_order_id' => 8757845345,
                    )
                )
            )
        );