php-shopify icon indicating copy to clipboard operation
php-shopify copied to clipboard

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

Open mathieu-coingt opened this issue 2 years ago • 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.

mathieu-coingt avatar Dec 14 '22 19:12 mathieu-coingt

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!

tonytoms avatar Dec 19 '22 02:12 tonytoms

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 avatar Dec 19 '22 09:12 d3v1

@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!

mathieu-coingt avatar Dec 19 '22 15:12 mathieu-coingt

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,
]);

mathieu-coingt avatar Dec 19 '22 15:12 mathieu-coingt

@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

tonytoms avatar Dec 23 '22 03:12 tonytoms

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);
}

d3v1 avatar Dec 23 '22 09:12 d3v1

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

tonytoms avatar Dec 23 '22 22:12 tonytoms

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

t-prod avatar Jan 09 '23 07:01 t-prod

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 avatar Jan 27 '23 23:01 williamoconnorme

@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.

t-prod avatar Feb 02 '23 07:02 t-prod

Please check the latest release

tareqtms avatar Feb 13 '23 03:02 tareqtms

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?

end2endi avatar Feb 21 '23 10:02 end2endi

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

tonytoms avatar Feb 21 '23 11:02 tonytoms

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

IMPMAC avatar Feb 21 '23 11:02 IMPMAC

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

end2endi avatar Feb 21 '23 11:02 end2endi

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']

tonytoms avatar Feb 23 '23 01:02 tonytoms

As was stated by Ismail Patel - if you don't have the right access scopes defined, you can't see fulfillment orders, but you just get a blank array rather than an error message!

I was trying to read / write merchant managed fulfillment orders but didn't have these scopes enabled so was getting a blank array as others have:

write_merchant_managed_fulfillment_orders, read_merchant_managed_fulfillment_orders

Once I enabled them I could get the fulfillment orders correctly using the following curl:

curl -X GET "https://{my_store}}. myshopify.com/admin/api/2023-01/orders/{order_id}/fulfillment_orders.json" -H "X-Shopify-Access-Token:{my_access_token}"

Tony, note also that in your code you're using the endpoint "2022-01" in your 2nd request for fulfillment orders. Can't remember if fulfillment orders were implemented back in that version, anyhow it works with "2023-01"...

Having got the request working with curl, it also then worked with the library:

$order_id="1234567890";

$shop=new PHPShopify\ShopifySDK($store_config);

$fo=$shop->Order($order_id)->FulfillmentOrder()->get();

print_r($fo);

I hope that helps someone!

Tim

On Thu, Feb 23, 2023 at 1:15 AM Tony Toms @.***> wrote:

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 https://github.com/end2endi , try it and I think you will get the values.

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

Does anyone have any luck in getting the fulfillment orders by calling the order.json endpoint?

— Reply to this email directly, view it on GitHub https://github.com/phpclassic/php-shopify/issues/283#issuecomment-1441101059, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACERLZ4VCOZRGQ4HAQDO3NDWY222NANCNFSM6AAAAAAS64E6KU . You are receiving this because you are subscribed to this thread.Message ID: @.***>

rancey avatar Mar 16 '23 12:03 rancey

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

geekworm-com avatar Jul 08 '23 12:07 geekworm-com

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.

geekworm-com avatar Jul 08 '23 12:07 geekworm-com