2

How can I get the customer name from the billing information variable in the commerce-order-receipt.twig.html template of the Drupal 8 Commerce order confirmation e-mail?

3
  • How about debugging it? You can learn how here What is easiest way to debug variables in nodes, views, blocks and page twig templates? – No Sssweat Jun 23 '18 at 6:31
  • The array or the object will be printed in the email and it will not be controlled. – user86344 Jun 25 '18 at 19:21
  • 1
    Otherwise, you could use a preprocess and do your debuging there. Example: in your_theme.theme add function YOUR_THEME_preprocess_commerce_order_receipt(&$variables) { kint($variables['billing_information']); } – No Sssweat Jun 25 '18 at 20:05
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy