I'm trying to add a custom track url to the trackings, using extension attributes, i added these in extension_attributes.xml
<extension_attributes for="Magento\Sales\Api\Data\ShipmentTrackInterface">
<attribute code="track_url" type="string"/>
</extension_attributes>
<extension_attributes for="Magento\Sales\Api\Data\ShipmentTrackCreationInterface">
<attribute code="track_url" type="string"/>
</extension_attributes>
Cleared the generated codes and caches, interfaces are populating correctly, I used \Magento\Sales\Api\ShipOrderInterface::execute
to save the shipment:
$this->shipOrderService->execute(
$order->getId(),
$items,
$data->isCustomerNotify(),
false,
$comment,
[$data->getTracking()]
);
and the $data->getTracking()
has data looking like this:
{
"track_number": "1Z37X3X41217934386",
"title": "ups",
"carrier_code": "ups",
"extension_attributes": {
"track_url": "blah"
}
}
So everything looks fine and the extension attributes data are passed fine down to Magento\Framework\Model\AbstractExtensibleModel
, however in Magento\Framework\Api\AbstractSimpleObject
which is what Magento\Sales\Api\Data\ShipmentTrackExtension
extends from, $data
becomes an empty array, i still can’t figure out why this is happening, i’m in dev mode and shouldn’t need to run DI or something like that, any ideas?
My Answer:
It seems like you have set up the extension attributes correctly in your extension_attributes.xml file. However, the issue might be related to how the data is being passed and processed in the ShipOrderInterface::execute method.
One thing you can try is to debug the data being passed to the ShipOrderInterface::execute method to see if the extension attributes are being passed correctly. You can do this by adding some logging or debugging statements in the execute method to check the contents of the $data->getTracking() object.
If the data is being passed correctly, then the issue might be related to how the data is being processed in the ShipOrderService class. Make sure that the ShipOrderService class is correctly handling the extension attributes and saving them to the database.
If you are still facing issues, you may need to check if there are any customizations or overrides in your code that might be affecting the processing of the extension attributes. Additionally, you can try clearing the generated code and caches again to ensure that any changes are properly reflected.
Overall, debugging the data flow and processing in your code will help you identify the root cause of the issue and find a solution.
Rate this post
4 of 5 based on 8681 votesComments