0

I use the following code to add a tracking number to an order:

        $url = 'https://xxxxx/rest/V1/order/514/ship';

        $authorization = "Authorization: Bearer ".$json; //I get $json in other part of the code, this work fine
    
        $data = [
            "items" => [
                [
                    "order_item_id" => 614,
                    "qty" => 1
                ]
            ],

            "tracks" => [
                "track_number" =>"1111111111",
                "title" => "Pedido enviado", 
                "carrier_code" => "DHL"
            ]
        ];

        $data_string = json_encode($data);

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization ));
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);            
        $result = curl_exec($curl);
        curl_close($curl);
        $json2 = json_decode($result);

But I get the following error:

"Shipment Document Validation Error(s): Please, introduce a tracking number.

What is the problem?

Thank you very much!!!

3 Answers 3

0

Go to http://devdocs.magento.com/swagger/ > salesShipmentTrackRepositoryV1

JSON:

{
  "entity": {
    "order_id": 345,
    "parent_id": 1,
    "entity_id": 1,
    "weight": 0,
    "qty": 0,
    "description": "Tracking code Descripton",
    "track_number": "1234567",
    "title": "Custom Tracking Code",
    "carrier_code": "custom"
  }
}
  • Parent Id - parent_id: is the foreign key from sales_shipment table.
  • Entity Id - entity_id(optional): is the entity id of sales_shipment_track table.
  • Carrier Code: Custom (custom), DHL (dhl), Federal Express (fedex), etc..

You can take a look some tables: sales_shipment, sales_shipment_track.

UPDATE SHIPMENT TRACK CODE:

<?php

$userData = array("username" => "admin", "password" => "test1234");
$ch = curl_init("http://mage21.loc/rest/V1/integration/admin/token");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($userData))));

$token = curl_exec($ch);


$shiptrack = [
    "entity" => [
        "order_id"=> 347, //can find in `sales_shipment_track` and `sales_shipment`
        "parent_id" => 145, //can find in `sales_shipment_track` and `sales_shipment`
        "entity_id"=> 1, //can find in `sales_shipment_track`. Removing it if want to create a new shipment track.
        "weight"=> 0,
        "qty"=> 0,
        "description"=> "Tracking code Description",
        "track_number"=> "1234567",
        "title"=> "Custom Tracking Code",
        "carrier_code"=> "custom"
  ]
];



$ch = curl_init("http://magen21.loc/rest/V1/shipment/track");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($shiptrack));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));

$result = curl_exec($ch);

$result = json_decode($result, 1);
echo '<pre>';print_r($result);

CREATE NEW SHIPMENT TRACK: If we want to create the new shipment track, we need to remove entity_id param.

3
  • I try this but i get the same error: Shipment Document Validation Error(s): Please, introduce a tracking number. Sep 22, 2020 at 10:22
  • Please check I edited my answer. @IvannVerano Sep 22, 2020 at 10:30
  • i don´t find parent id :( When I call https:/xxxxx/rest/V1/orders?searchCriteria=all I get the data of all orders, but here there are many parent_id. Sep 22, 2020 at 13:00
0

When i do the call with Postman this works:

enter image description here

I know it works because the products information say shipped:

enter image description here

Why doesn't this work with the php code? :S

0

Old question, but I ran into the same problem and had trouble finding the answer.

So, in case anybody else runs into this issue, the "Please enter a tracking number" error is caused by the fact that "tracks" is set up to allow multiple tracking numbers, so it needs to be an array of objects.

Just surround the "tracks" data in another [array], like this.

        $data = [
            "items" => [
                [
                    "order_item_id" => 614,
                    "qty" => 1
                ]
            ],

            "tracks" => [
                [
                    "track_number" =>"1111111111",
                    "title" => "Pedido enviado", 
                    "carrier_code" => "DHL"
                ]
            ]
        ];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.