Problem/Motivation
LateOrderProcessor::process() saves shipments that changed during the order refresh, including shipments that were just created by the packer:
foreach ($shipments as $shipment) {
if ($this->shouldSave($shipment)) {
$shipment->save();
}
// …
}Saving the shipment does not update the entity reference the order holds for it. For a shipment that was new when $order->set('shipments', …) ran in EarlyOrderProcessor, the field item keeps target_id = NULL and only holds the entity object.
This normally goes unnoticed because EntityReferenceItem::preSave() runs afterwards on the usual save path and backfills the ID. It breaks whenever the refresh runs after the order's fields were pre-saved — for example from a commerce_order.commerce_order.presave subscriber, since ContentEntityStorageBase::invokeHook() calls invokeFieldMethod('preSave', …) before dispatching module hooks and events.
The order write then fails:
SQLSTATE[23000]: Integrity constraint violation: 1048
Column 'shipments_target_id' cannot be nullProposed resolution
Have LateOrderProcessor put the saved shipments back on the order, so the field is left consistent by the processor that saved them:
$order->set('shipments', $shipments);
On the usual save path this is a no-op, the field is re-set to values it already holds, so the change is safe regardless of when the refresh runs.
Remaining tasks
Review, decide whether a kernel test asserting the order references a shipment created during refresh is wanted.
Issue fork commerce_shipping-3620685
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
bl457xor commentedComment #4
bl457xor commentedMR opened against 3.x.
The change is one line at the end of LateOrderProcessor::process():
Why it belongs there. process() already saves shipments that changed during the refresh, including ones the packer just created, since shouldSave() is hasTranslationChanges() and that returns TRUE for a new entity. Saving the shipment does not update the reference the order holds for it: the field item still has target_id = NULL and only the entity object, because EntityReferenceItem::onChange() fires when the entity property is assigned, not when the referenced entity is later saved. The processor that saves the shipments should leave the field consistent.
Why it is normally invisible. On the usual save path EntityReferenceItem::preSave() runs after the refresh and backfills the ID. It only surfaces when the refresh happens after the order's fields were pre-saved, for instance from a commerce_order.commerce_order.presave subscriber, since ContentEntityStorageBase::invokeHook() calls invokeFieldMethod('preSave', …) before dispatching module hooks and events. The order write then fails with:
Risk. None on the normal path: the field is re-set to the values it already holds, so hasTranslationChanges() on the order is unaffected and nothing extra is saved.
Related issues:
#3328162: Non draft orders are refreshed when editing shipments
#2875804: Order Refresh should work for non-draft orders, too
Happy to add a kernel test asserting the order references a shipment created during the refresh if that would help this land.
Comment #6
jsacksick commentedTrusting you on this! Merged!