Loading an order item's order when the order needs refreshing crashes with Argument 1 passed to Drupal\commerce_tax\Plugin\Commerce\TaxType\TaxTypeBase::buildCustomerProfile() must implement interface Drupal\commerce_order\Entity\OrderInterface

Created on 10 May 2021, over 3 years ago
Updated 29 September 2023, about 1 year ago

Describe your bug or feature request.

This is a bug I am seeing during a migration, with a migrated order. So it's possible this is only happening because of a weird combination of data that doesn't normally happen.

I am reproducing this bug with code that just tries to load the order from the order item, like this:

  $item = $entityTypeManager->getStorage('commerce_order_item')->load(35559);
  $item->getOrder();

This is the error:

TypeError: Argument 1 passed to Drupal\commerce_tax\Plugin\Commerce\TaxType\TaxTypeBase::buildCustomerProfile() must implement interface Drupal\commerce_order\Entity\OrderInterface, null given, called in /Users/joachim/Sites/training-cloud/web/modules/contrib/commerce/modules/tax/src/Plugin/Commerce/TaxType/TaxTypeBase.php on line 246 in Drupal\commerce_tax\Plugin\Commerce\TaxType\TaxTypeBase->buildCustomerProfile() (line 271 of modules/contrib/commerce/modules/tax/src/Plugin/Commerce/TaxType/TaxTypeBase.php).

Drupal\commerce_tax\Plugin\Commerce\TaxType\TaxTypeBase->buildCustomerProfile(NULL) (Line: 246)
Drupal\commerce_tax\Plugin\Commerce\TaxType\TaxTypeBase->resolveCustomerProfile(Object) (Line: 118)
Drupal\commerce_tax\Plugin\Commerce\TaxType\LocalTaxTypeBase->apply(Object) (Line: 60)
Drupal\commerce_tax\TaxOrderProcessor->process(Object) (Line: 162)
Drupal\commerce_order\OrderRefresh->refresh(Object) (Line: 131)
Drupal\commerce_order\OrderStorage->doOrderPreSave(Object) (Line: 108)
Drupal\commerce_order\OrderStorage->invokeHook('presave', Object) (Line: 500)
Drupal\Core\Entity\EntityStorageBase->doPreSave(Object) (Line: 700)
Drupal\Core\Entity\ContentEntityStorageBase->doPreSave(Object) (Line: 454)
Drupal\Core\Entity\EntityStorageBase->save(Object) (Line: 837)
Drupal\Core\Entity\Sql\SqlContentEntityStorage->save(Object) (Line: 395)
Drupal\Core\Entity\EntityBase->save() (Line: 164)
Drupal\commerce_order\OrderStorage->postLoad(Array) (Line: 307)
Drupal\Core\Entity\EntityStorageBase->loadMultiple(Array) (Line: 250)
Drupal\Core\Entity\EntityStorageBase->load('14244') (Line: 72)
Drupal\Core\Entity\Plugin\DataType\EntityReference->getTarget() (Line: 37)
Drupal\Core\TypedData\DataReferenceBase->getValue() (Line: 140)
Drupal\Core\Field\FieldItemBase->__get('entity') (Line: 116)
Drupal\Core\Field\FieldItemList->__get('entity') (Line: 57)
Drupal\commerce_order\Entity\OrderItem->getOrder() (Line: 59)

The backtrace doesn't properly show what the problem is though.

When we get to OrderItem::getOrder, this line:

return $this->get('order_id')->entity;

causes this error which is picked up by Drupal's error handler:

> "Undefined property: Drupal\Core\Field\EntityReferenceFieldItemList::$entity"

The reason this is failing is that $entity is a property that doesn't exist, which FieldItemList::__get() then picks up.

But this won't work here, because we already used the magic __get() method in the original call to OrderItem->getOrder().

Once you're inside a magic method call for a property, it won't get called again, and so $entity is not found.

Hence the inner call to getOrder() fails, and TaxTypeBase::buildCustomerProfile() doesn't have an order to work with.

If a bug, provide steps to reproduce it from a clean install.

🐛 Bug report
Status

Active

Version

2.0

Component

Order

Created by

🇬🇧United Kingdom joachim

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024