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 null

Proposed 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.

Command icon 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

bl457xor created an issue. See original summary.

bl457xor’s picture

Status: Active » Needs review
bl457xor’s picture

MR opened against 3.x.

The change is one line at the end of LateOrderProcessor::process():

$order->set('shipments', $shipments);

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:

SQLSTATE[23000]: Integrity constraint violation: 1048
Column 'shipments_target_id' cannot be null

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.

  • jsacksick committed c660e27e on 3.x authored by bl457xor
    fix: #3620685 LateOrderProcessor saves shipments without updating the...
jsacksick’s picture

Status: Needs review » Fixed

Trusting you on this! Merged!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.