Unable to add/persist tracking data with magento 2 rest api
-
26-01-2021 - |
Question
There is a store that I'm posting shipments to via the rest api. I'm POSTing to the /V1/orders/<order_number>
/ship endpoint. My post data looks like:
{
"items":
[
{
"orderItemId": 123456,
"qty": 3
}
],
"tracks":
[
{
"track_number": 12341234123423
"carrier_code": "FEDEXG"
}
]
}
and when I query the api to look at my shipment, the shipment is there, but tracks
is an empty array []
. If I try to add the tracking data after the fact with /V1/shipments/track,
and the following data:
{
"entity": {
"order_id": 123456,
"entity_id": 12345,
"track_number": 123123123123,
"carrier_code": "FEDEXG"
}
}
i get the following error message:
{
"message": "Could not save the shipment tracking.",
"trace": "#0 [internal function]: Magento\\Sales\\Model\\Order\\Shipment\\TrackRepository->save(Object(Magento\\Sales\\Model\\Order\\Shipment\\Track))\n#1
/home/company/public_html/vendor/magento/module-webapi/Controller/Rest.php(330): call_user_func_array(Array, Array)\n#2
/home/company/public_html/vendor/magento/module-webapi/Controller/Rest.php(239): Magento\\Webapi\\Controller\\Rest->processApiRequest()\n#3
/home/company/public_html/vendor/magento/framework/Interception/Interceptor.php(58): Magento\\Webapi\\Controller\\Rest->dispatch(Object(Magento\\Framework\\App\\Request\\Http))\n#4
/home/company/public_html/vendor/magento/framework/Interception/Interceptor.php(138): Magento\\Webapi\\Controller\\Rest\\Interceptor->___callParent('dispatch', Array)\n#5
/home/company/public_html/vendor/magento/framework/Interception/Interceptor.php(153): Magento\\Webapi\\Controller\\Rest\\Interceptor->Magento\\Framework\\Interception\\{closure}(Object(Magento\\Framework\\App\\Request\\Http))\n#6
/home/company/public_html/generated/code/Magento/Webapi/Controller/Rest/Interceptor.php(39): Magento\\Webapi\\Controller\\Rest\\Interceptor->___callPlugins('dispatch', Array, Array)\n#7
/home/company/public_html/vendor/magento/framework/App/Http.php(135): Magento\\Webapi\\Controller\\Rest\\Interceptor->dispatch(Object(Magento\\Framework\\App\\Request\\Http))\n#8
/home/company/public_html/generated/code/Magento/Framework/App/Http/Interceptor.php(24): Magento\\Framework\\App\\Http->launch()\n#9 /home/company/public_html/vendor/magento/framework/App/Bootstrap.php(256): Magento\\Framework\\App\\Http\\Interceptor->launch()\n#10
/home/company/public_html/index.php(39): Magento\\Framework\\App\\Bootstrap->run(Object(Magento\\Framework\\App\\Http\\Interceptor))\n#11 {main}"
}
Anyone know what I am doing wrong here? I'd be fine with either adding tracking data at the time I make the shipment, or adding it after. I just need a way to add tracking data to the shipment.
Solution 2
The solution was that my tracking data was improperly formatted. I needed to post data like this:
{
"entity":
{
"order_id": 123456,
"parent_id": 12345,
"trackNumber": 123123123123,
"carrierCode": "FEDEXG",
"title": "fedex"
}
}
OTHER TIPS
When you call /V1/shipments/track
the code that executes is inside
Magento\Sales\Model\Order\Shipment\TrackRepository
and looks like
public function save(ShipmentTrackInterface $entity)
{
try {
$this->trackResource->save($entity);
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
throw new CouldNotSaveException(__('Could not save the shipment tracking.'), $e);
}
return $entity;
}
So, you see the Could not save...
message, but ALSO this is called
$this->logger->error($e->getMessage());
which means, there is a log file somewhere that actually has captured the PHP error that occurred. Please try to locate this log entry. It is your best bet.
As far as myself, not having access to that log info, my bet is that there are properties that are required but missing from the object that is attempted to be saved.
My reasoning goes like:
This code
$this->trackResource->save($entity);
actually calls save()
from
Magento\Sales\Model\ResourceModel\Order\Shipment\Track
And this resource model actually maps to database table
sales_shipment_track
And if I look at the structure of the table the following properties are not null-able:
- parent_id
- order_id
- created_at
- updated_at
Any chance that you are not sending time signatures for created_at
or updated_at
. Or, you are never setting the parent_id
(whatever it is)?
I hope this helps.