Fatal error when cart is in odd state

Created on 1 October 2023, 9 months ago

Problem/Motivation

I have a D10 Commerce site that I installed the module on, once I configured a registration type, I immediately started getting this error on any page that seems to be trying to load a cart:

TypeError: Drupal\registration\RegistrationHostEntityHandler::createHostEntity(): Argument #1 ($entity) must be of type Drupal\Core\Entity\EntityInterface, null given, called in /var/www/html/web/modules/contrib/commerce_registration/src/OrderProcessor/RegistrationOrderProcessor.php on line 47 in Drupal\registration\RegistrationHostEntityHandler->createHostEntity() (line 21 of modules/contrib/registration/src/RegistrationHostEntityHandler.php).
Drupal\commerce_registration\OrderProcessor\RegistrationOrderProcessor->process(Object) (Line: 189)
Drupal\commerce_order\OrderRefresh->refresh(Object) (Line: 113)
Drupal\commerce_order\OrderStorage->doOrderPreSave(Object) (Line: 78)
Drupal\commerce_order\OrderStorage->invokeHook('presave', Object) (Line: 529)
Drupal\Core\Entity\EntityStorageBase->doPreSave(Object) (Line: 753)
Drupal\Core\Entity\ContentEntityStorageBase->doPreSave(Object) (Line: 483)
Drupal\Core\Entity\EntityStorageBase->save(Object) (Line: 806)
Drupal\Core\Entity\Sql\SqlContentEntityStorage->save(Object) (Line: 159)
Drupal\commerce_order\OrderStorage->save(Object) (Line: 339)
Drupal\Core\Entity\EntityBase->save() (Line: 146)
Drupal\commerce_order\OrderStorage->postLoad(Array) (Line: 319)
Drupal\Core\Entity\EntityStorageBase->loadMultiple(Array) (Line: 1630)
Drupal\views\Plugin\views\query\Sql->loadEntities(Array) (Line: 1555)
Drupal\views\Plugin\views\query\Sql->execute(Object) (Line: 1441)
Drupal\views\ViewExecutable->execute(NULL) (Line: 1469)
Drupal\views\ViewExecutable->render() (Line: 199)
Drupal\views\Plugin\views\display\Page->execute() (Line: 1645)
Drupal\views\ViewExecutable->executeDisplay('page_1', Array) (Line: 81)
Drupal\views\Element\View::preRenderViewElement(Array)
call_user_func_array(Array, Array) (Line: 111)
Drupal\Core\Render\Renderer->doTrustedCallback(Array, Array, 'Render #pre_render callbacks must be methods of a class that implements \Drupal\Core\Security\TrustedCallbackInterface or be an anonymous function. The callback was %s. See https://www.drupal.org/node/2966725', 'exception', 'Drupal\Core\Render\Element\RenderCallbackInterface') (Line: 788)
Drupal\Core\Render\Renderer->doCallback('#pre_render', Array, Array) (Line: 377)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 204)
Drupal\Core\Render\Renderer->render(Array, ) (Line: 238)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 583)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 239)
Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 128)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
call_user_func(Array, Object, 'kernel.view', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, 'kernel.view') (Line: 171)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 74)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 1, 1) (Line: 704)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Steps to reproduce

I think the steps here are to have a WIP cart when installing the module. In this case it's because I have an admin cart from messing around with Commerce Kickstart. I believe that cart may have a reference to a deleted product in it.

Proposed resolution

I'll probably resolve this by cleaning up that cart from the command line or something, or possibly uninstall the module, clean it up, then reinstall, but I believe the module code could avoid erroring with some checks in the referenced code.

Simply changing those lines to this:

<?php
      /** @var \Drupal\commerce_product\Entity\ProductVariationInterface $variation */
      $variation = $item->getPurchasedEntity();
      if ($variation) {
        $host_entity = $handler->createHostEntity($variation);
      }
?>

removes the fatal error.

Remaining tasks

Decide what direction is desired to fix this edge case.

πŸ› Bug report
Status

Closed: duplicate

Version

3.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States dustinleblanc Ithaca, NY

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

Comments & Activities

Production build 0.69.0 2024