8 – How to get the customer name in Commerce order confirmation e-mail?

First, you have to create function as @NoSSweat suggested (thanks for the hint) and from it get it like:

function hook_preprocess_commerce_order_receipt(&$variables)
{
  $givenName = $variables('billing_information')('#profile')->get('address')(0)->given_name;
  $variables('given_name') = $givenName;
}

(was incredible hard to figure this out 🙂 ) Field name for last name would be “family_name”. Checkout the file:

/modules/contrib/address/src/Plugin/Field/FieldType/AddressItem.php

for other available fields.

I added this function to my module, but adding it to theme file should also work.

That way you are assigning variable which is now available in twig template so you can print it simple as:

{{ given_name }}

Don’t forget to clear the cache.