Reputation: 177
I have an issue regarding a subscriber for the OrderWrittenEvent. I was able to get the operation of an order if it was created or updated. But now I only get an "insert" even when I update an existing order through the administration.
Is this behaviour intended? If so, how can I check if the order is only updated and not new?
public function onOrderWritten(EntityWrittenEvent $event): void
{
if ($event->getEntityName() !== OrderDefinition::ENTITY_NAME
|| $event->getWriteResults()[0]->getOperation() == 'update') {
return;
}
...
I hope someone can shed some light. :)
Thanks,
Danny
Upvotes: 0
Views: 81
Reputation: 13161
When you edit an order in the administration, the order is cloned (same data but inserted as a new database record) under a new version id. Hence why you see an insert operation where you might expect an update operation.
Once the edits of the order are saved the updated new version and the original version will be merged into a single record. This is done so changes to orders are versionized and may be reviewed or reverted. If you need to find out whether an insert/update is of a new version of an order you can check if the version id is not the same as the live version. This way you will know that it is not an actual new order, but the beginning of an edit.
foreach ($event->getWriteResults() as $writeResult) {
$keys = $writeResult->getPrimaryKey();
$versionId = $keys['versionId'];
if ($versionId !== Defaults::LIVE_VERSION) {
// insert or update is part of the process of editing an order
}
}
Upvotes: 2