I'm not sure if we'll be able to resolve this, but documenting it here for reference by an issue in another contrib module that works around it:

Problem/motivation:

Order item does not have its backreference to its order until after the Order is saved - ref Order::postSave.

This means that the OrderItem entity in the OrderEvents::ORDER_ITEM_INSERT event does not have a reference to it's order_id yet.

Proposed resolution:

Populate the backreference to the order before the order item is saved?

Comments

steveoliver created an issue. See original summary.

mglaman’s picture

We treat order items as an embedded entity. We do this in the refresh process - order items are refreshed when the order is handled. So any action on order items should happen when the order is also saved.

If an order item is created the order itself will soon be saved, as it needs its price recalculated.

I wonder if >addItem on the Order class should at least populate the order ID. Maybe that would be the compromise?

lisastreeter’s picture

Issue tags: +midcamp2018
StatusFileSize
new2.53 KB

I've modified the CartManager class to set the order id for an item when the item is added to the cart.

lisastreeter’s picture

StatusFileSize
new1.59 KB

Simpler, less disruptive approach.

mglaman’s picture

Status: Active » Needs review
+++ b/modules/cart/src/CartManager.php
@@ -113,6 +113,7 @@ class CartManager implements CartManagerInterface {
+      $order_item->set('order_id', $cart->id());

At first review I thought this should go in addEntity. But the AddToCartForm actually invokes addOrderItem since it is an order item form.

This makes sense to me, then.

josephdpurcell’s picture

Status: Needs review » Reviewed & tested by the community

I looked at the code and it looks correct: it adds the order id to the order item always only when its a new item added to the cart. And there is a test. And the patch applies. Setting RTBC.

mglaman’s picture

Status: Reviewed & tested by the community » Needs review

Kicking back to Needs Review. I should have commented this before, but I think we really need feedback from steveoliver and the commerce_stock folk. I believe they need this most, and we need to validate this approach works for them.

olafkarsten’s picture

Status: Needs review » Reviewed & tested by the community

#4 looks good. It solves the issue for stock. Thanks!

olafkarsten’s picture

Sorry, need to correct myself. The original idea for commerce_stock was to act on OrderEvents::ORDER_ITEM_INSERT. That would require the backreference to the parent order is always there before it gets inserted. I don't think we will resolve this and I don't think its necessary for stock. It's just the wrong event for commerce_stock to act on. Note: this is not a critical issue for stock. The "work around" mentioned in the issue summary is in fact the correct approach.

  • bojanz committed ac5cb9b on 8.x-2.x authored by lisastreeter
    Issue #2853277 by lisastreeter: Populate OrderItem backreference to...
bojanz’s picture

Status: Reviewed & tested by the community » Fixed

Thanks!

Status: Fixed » Closed (fixed)

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

joachim’s picture

Is this code in Order now redundant in normal operations?

  public function postSave(EntityStorageInterface $storage, $update = TRUE) {
    parent::postSave($storage, $update);

    // Ensure there's a back-reference on each order item.
    foreach ($this->getItems() as $order_item) {
      if ($order_item->order_id->isEmpty()) {
        $order_item->order_id = $this->id();
        $order_item->save();
      }
    }
  }

It should ideally stay though, as Commerce Migrate relies on it.