phpclassic/php-shopify

How to fulfill an order with Shopify API 2022-07 and above ?

mathieu-coingt opened this issue ยท 19 comments

With Shopify API 2022-01, I fulfilled my orders with these instructions :

$shopify->Order($orderID)->Fulfillment->post([
      "tracking_number" => 'XXXXXXX'
      "tracking_urls" => 'https://tracking_url...',
      "notify_customer" => true,
      "location_id" => 'location_id,
]);

With Shopify API 2022-07 and above, this is deprecated as it's mentioned in others issues, but even with the doc and some samples, I'm not able to convert this code to be compliant with the new fulfillment API.

Is someone could provide me an exemple ?

Thank you.

Hey Mathieu,
I am on the same page as you are Are you sure this is deprecated? The reason I ask this is because of the below suggestion I got from the Shopify community
https://community.shopify.com/c/payments-shipping-and/using-fulfillment-orders-api-instead-of-fulfilment-api/m-p/1852743#M67304
The suggested solution was this "You can use the POST /admin/api/2022-01/orders/#{id}/fulfillments.json endpoint to update the fulfillment status of an order"

$shopify->Order($orderID)->Fulfillment->post uses the below POST link
https://XXXXXXX/admin/api/2021-01/orders/11111111/fulfillments.json
Which is the solution they are suggesting.
Please do correct me if I am wrong!

d3v1 commented

I've been using the code below. It took me a while to get the permissions right on the API user, it needed all of the permissions for managing tracking including third party tracking and also reading/writing orders.

If you you need to get the fulfillment ID from the order and then use the fulfillment id to update the tracking info.

 $shopify->Fulfillment($fulfillment['id'])->update_tracking([
      "tracking_number" => 'XXXXXXX'
      "tracking_urls" => 'https://tracking_url...',
      "notify_customer" => true,
      "location_id" => 'location_id,
]);

@tonytoms this is deprecated since Shopify API 2022-07.
It's respond with a 404 error.

Hey Mathieu, I am on the same page as you are Are you sure this is deprecated? The reason I ask this is because of the below suggestion I got from the Shopify community https://community.shopify.com/c/payments-shipping-and/using-fulfillment-orders-api-instead-of-fulfilment-api/m-p/1852743#M67304 The suggested solution was this "You can use the POST /admin/api/2022-01/orders/#{id}/fulfillments.json endpoint to update the fulfillment status of an order"

$shopify->Order($orderID)->Fulfillment->post uses the below POST link https://XXXXXXX/admin/api/2021-01/orders/11111111/fulfillments.json Which is the solution they are suggesting. Please do correct me if I am wrong!

Thank you @d3v1 this is probably the solution.
Since my message I tried with the official Shopify SDK, and it's the way to do.

I've been using the code below. It took me a while to get the permissions right on the API user, it needed all of the permissions for managing tracking including third party tracking and also reading/writing orders.

If you you need to get the fulfillment ID from the order and then use the fulfillment id to update the tracking info.

 $shopify->Fulfillment($fulfillment['id'])->update_tracking([
      "tracking_number" => 'XXXXXXX'
      "tracking_urls" => 'https://tracking_url...',
      "notify_customer" => true,
      "location_id" => 'location_id,
]);

@d3v1
Where did you get the Fulfilment ID from? Did you get it from the 'GET fulfillment_orders endpoint?'. If yes , could you give me an example if possible

d3v1 commented

Hey @tonytoms

I got the fulfillment ID from the order. In this case we're using the Fulfillment object. It's different to the FulfillmentOrder object. I believe the FulfillmentOrder object will replace the Fulfillment object in a future release, I couldn't see any FulfillmentOrders on our transactions.

Here's an example bit of code you can start with:

$config = array(
    'ShopUrl' => 'your-shop-name-here.myshopify.com',
    'AccessToken' => YOUR_API_KEY_GOES_HERE,
    'ApiVersion' => '2022-10',
);

PHPShopify\ShopifySDK::config($config);

$shopify = new PHPShopify\ShopifySDK;

// get the order
$order = $shopify->Order($theOrderIdHere)->get();

$fulfillments = $order['fulfillments'];

foreach ($fulfillments as $fulfillment) {

    // fulfillment id is below, we don't need to assing it to a variable, I thought it'd help make it clearer
    $fulfillmentId = $fulfillment['id'];

    // build your payload here
    $payload = [
        'fulfillment' => [
            'notify_customer' => true, // you may want to make this false if you're correcting a tracking issue
            "tracking_info" => [
                'number' => 'NEW_TRACKING_NUMBER_HERE',
                'company' => 'SHIPPING_COMPANY_HERE',
                'url' => 'https://the-full-tracking-url-here.com/with/path?and=THE_QUERY_STRING'
            ]
        ]
    ];

    // submit it to shopify
    $result = $shopify->Fulfillment($fulfillmentId)->update_tracking($payload);

    // the result is the new fulfillment data in case you want to store it somewhere or check the update worked
    // print_r($result);
}

Hey @tonytoms

I got the fulfillment ID from the order. In this case we're using the Fulfillment object. It's different to the FulfillmentOrder object. I believe the FulfillmentOrder object will replace the Fulfillment object in a future release, I couldn't see any FulfillmentOrders on our transactions.

Here's an example bit of code you can start with:

$config = array(
    'ShopUrl' => 'your-shop-name-here.myshopify.com',
    'AccessToken' => YOUR_API_KEY_GOES_HERE,
    'ApiVersion' => '2022-10',
);

PHPShopify\ShopifySDK::config($config);

$shopify = new PHPShopify\ShopifySDK;

// get the order
$order = $shopify->Order($theOrderIdHere)->get();

$fulfillments = $order['fulfillments'];

foreach ($fulfillments as $fulfillment) {

    // fulfillment id is below, we don't need to assing it to a variable, I thought it'd help make it clearer
    $fulfillmentId = $fulfillment['id'];

    // build your payload here
    $payload = [
        'fulfillment' => [
            'notify_customer' => true, // you may want to make this false if you're correcting a tracking issue
            "tracking_info" => [
                'number' => 'NEW_TRACKING_NUMBER_HERE',
                'company' => 'SHIPPING_COMPANY_HERE',
                'url' => 'https://the-full-tracking-url-here.com/with/path?and=THE_QUERY_STRING'
            ]
        ]
    ];

    // submit it to shopify
    $result = $shopify->Fulfillment($fulfillmentId)->update_tracking($payload);

    // the result is the new fulfillment data in case you want to store it somewhere or check the update worked
    // print_r($result);
}

Perfection. Thank you so much. Appreciate it

So the solution is to use the Shopify PHP SDK ? No updates on this package ?

I echo @t-prod question as the FulfillmentOrder API is important for order fulfillment in later versions of the Shopify REST API. There is a lot of new functionality too for applying, retrieving and releasing fulfillment holds

@williamoconnorme I think we should use the Shopify PHP SDK now for this feature I don't believe there's a planified development for this feature cause the deadline is in April.

Please check the latest release

What do you do if you don't have any fulfillment_id's tied to the order. When I query an order for fulfillments, I receive an empty array. Clearly I need to create a fulfillment but I can't find any documentation on this that doesn't include the newer FulfillmentOrder API. Could someone point me in the right direction for creating the request?

What do you do if you don't have any fulfillment_id's tied to the order. When I query an order for fulfillments, I receive an empty array. Clearly I need to create a fulfillment but I can't find any documentation on this that doesn't include the newer FulfillmentOrder API. Could someone point me in the right direction for creating the request?

I am facing the same issue. From the official website I can see that we need to grand some permissions to the app. I haven't tried it but I hope that's why the array is null.
There is also a clause that says
Fulfillment orders are created automatically when an order is created.
Let me know if anyone else think the issue is because of that?
I will try it in a few days time anyways.
You can see the permissions in the below migration document .

https://shopify.dev/docs/apps/fulfillment/migrate

I had the same problem, I needed to add the additional scopes and then the array wasn't empty. If you don't have the correct scopes, it doesn't throw any error, it just returns empty

I have the additional scopes set and I still get an empty array, unless the order has already shipped.

I have the additional scopes set and I still get an empty array unless the order has already shipped.

I just tried the same and am also getting no fulfillments inside the object of the order.
The endpoint I am using is
https://myshopify.com/admin/api/2023-01/orders.json?fulfillment_status=unfulfilled
However, I am getting the fulfilment order IDs when calling the Fulfillment get api separately . @end2endi , try it and I think you will get the values.

https://myshopify.com/admin/api/2023-01/orders/4779611111/fulfillment_orders.json

Does anyone have any luck in getting the fulfillment orders by calling the order.json endpoint like below?
$order = $shopify->Order($theOrderIdHere)->get();
$fulfillments = $order['fulfillments']

I implemented the function of shipping orders on the 2023-04 version. The sample code is as follows:

static function create_order_fulfillment_new($order_id, $notify_customer, $tracking_number, $tracking_company) {
    $config = array(
                'ShopUrl' => self::MYSHOPIFY_DOMAIN,
                'ApiKey' => self::API_KEY,
                'Password' => self::PASSWORD,
                'ApiVersion' => '2023-04',
                // The following must be added, refer to https://github.com/phpclassic/php-shopify/issues/187
                'AccessToken' => self::PASSWORD,
                'Curl' => array(
                    CURLOPT_SSL_VERIFYHOST => false,
                    CURLOPT_SSL_VERIFYPEER => false,
                    CURLOPT_TIMEOUT => 10,
                )
            );
    $sp= new PHPShopify\ShopifySDK($config);

    // Only get the FulfillmentOrder when declare the shipping order.
    $list = $sp->Order($order_id)->FulfillmentOrder()->get();
    $fulfillmentorder_id = $list[0]['id'];

    $params = array(
                'notify_customer'=> $notify_customer,
                'tracking_info' => [
                    'number' => $tracking_number,
                    'company' => $tracking_company
                ],
                'line_items_by_fulfillment_order' => [
                        array('fulfillment_order_id' => $fulfillmentorder_id)
                ]
            );
    $ret = $sp->Fulfillment()->post($params);
    print_r($ret);
}

If you find some bug, please point it out, thanks

I implemented the function of shipping orders on the 2023-04 version. The sample code is as follows:

static function create_order_fulfillment_new($order_id, $notify_customer, $tracking_number, $tracking_company) {
    $config = array(
                'ShopUrl' => self::MYSHOPIFY_DOMAIN,
                'ApiKey' => self::API_KEY,
                'Password' => self::PASSWORD,
                'ApiVersion' => '2023-04',
                // The following must be added, refer to https://github.com/phpclassic/php-shopify/issues/187
                'AccessToken' => self::PASSWORD,
                'Curl' => array(
                    CURLOPT_SSL_VERIFYHOST => false,
                    CURLOPT_SSL_VERIFYPEER => false,
                    CURLOPT_TIMEOUT => 10,
                )
            );
    $sp= new PHPShopify\ShopifySDK($config);

    // Only get the FulfillmentOrder when declare the shipping order.
    $list = $sp->Order($order_id)->FulfillmentOrder()->get();
    $fulfillmentorder_id = $list[0]['id'];

    $params = array(
                'notify_customer'=> $notify_customer,
                'tracking_info' => [
                    'number' => $tracking_number,
                    'company' => $tracking_company
                ],
                'line_items_by_fulfillment_order' => [
                        array('fulfillment_order_id' => $fulfillmentorder_id)
                ]
            );
    $ret = $sp->Fulfillment()->post($params);
    print_r($ret);
}

If you find some bug, please point it out, thanks

I googled or referred to many other online methods, but none of them succeeded, or I felt that it was not easy to use. Finally, directly refer to the official https://shopify.dev/docs/api/admin-rest/2023-04/resources/fulfillment#post-fulfillments, and then construct the corresponding parameters.
Of course, thanks to the SDK library php-shopify.