My drupal commande and drupal are up to date.
I have tested with drupal commerce development version and same problem.

When I edit an order, I have this error.
Sometimes it works, sometimes it didn't. There is no difference between my order i don't understand why is bugued.

The website encountered an unexpected error. Please try again later.
Error: Call to a member function getCountryCode() on null in Drupal\commerce_order\AddressBook->isAvailable() (line 189 of modules/contrib/commerce/modules/order/src/AddressBook.php).

Drupal\commerce_order\AddressBook->isAvailable(Object, Array) (Line: 99)
Drupal\commerce_order\AddressBook->loadAll(Object, 'customer', Array) (Line: 169)
Drupal\commerce_order\Plugin\Commerce\InlineForm\CustomerProfile->buildInlineForm(Array, Object) (Line: 111)
Drupal\commerce_order\Plugin\Field\FieldWidget\BillingProfileWidget->formElement(Object, 0, Array, Array, Object) (Line: 335)
Drupal\Core\Field\WidgetBase->formSingleElement(Object, 0, Array, Array, Object) (Line: 202)
Drupal\Core\Field\WidgetBase->formMultipleElements(Object, Array, Object) (Line: 104)
Drupal\Core\Field\WidgetBase->form(Object, Array, Object) (Line: 177)
Drupal\Core\Entity\Entity\EntityFormDisplay->buildForm(Object, Array, Object) (Line: 125)
Drupal\Core\Entity\ContentEntityForm->form(Array, Object) (Line: 61)
Drupal\commerce_order\Form\OrderForm->form(Array, Object) (Line: 117)
Drupal\Core\Entity\EntityForm->buildForm(Array, Object)
call_user_func_array(Array, Array) (Line: 519)
Drupal\Core\Form\FormBuilder->retrieveForm('commerce_order_default_edit_form', Object) (Line: 276)
Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 93)
Drupal\Core\Controller\FormController->getContentResult(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 582)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 151)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
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: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 693)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Comments

Asterovim created an issue. See original summary.

bojanz’s picture

Category: Bug report » Support request
Priority: Critical » Normal

Open the file throwing the error (AddressBook.php), and go to the line in question (189). It is the return line:

  protected function isAvailable(ProfileInterface $profile, array $available_countries) {
    if (empty($available_countries)) {
      return TRUE;
    }
    /** @var \Drupal\address\Plugin\Field\FieldType\AddressItem $address */
    $address = $profile->get('address')->first();

    return in_array($address->getCountryCode(), $available_countries);
  }

Since $address is NULL, it tells you that you have a broken $profile somewhere in the system, one without an address field, which should be impossible, since the address field is required.

Asterovim’s picture

Hello Bonjanz and thanks you.
My website use only one country.

I have commented all the protected function isAvailable(ProfileInterface $profile, array $available_countries)
And now i can see all the order informations. The user profile is not empty.

I think it could be because i don't use the address field with my customer. I have make it private field and all field of address are hidden.

This was not the case before. It's a new bug :(

  • bojanz committed 26baf9e on 8.x-2.x
    Issue #3095413 by bojanz: Error: Call to a member function...
bojanz’s picture

Category: Support request » Bug report
Status: Active » Fixed

Pushed a fix for the crash in question. There is no reason not to account for an empty address here.

However, keep in mind that you will see other crashes over time, because Commerce was not designed to have profiles without an address. For example, many payment gateways will crash. You can always make sure that your address at least has the country code set, to avoid that problem.

Note that payment gateways have a "Collect billing information" checkbox (#2905028: Add a per-gateway setting to skip collecting billing information) that allows you to configure not collecting billing information (so no $profile at all), but it sounds like you are still collecting other information on the profile.

Status: Fixed » Closed (fixed)

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

Beulette’s picture

Hi, I'm getting the same error and can't process to my updates from 2.14 to 2.16 because of it, is there a solution how to add the address field that is missing ?

Beulette’s picture

Ok, actually I have the field when I take a look in back-office.
Maybe the commerce paypal module isn't using it the good way, most required fields aren't populated.