Basic cart tokens in Drupal

A short note on the topic of module tokens.

The Drupal 8 Basic Cart has an email alert for a new order. You can insert order field tokens into an email, but not all of them work.

A crutch solution found at drupal.org.

Go to the module folder and edit the basic_cart.module file, look for the function basic_cart_tokens ($ type, $ tokens, array $ data, array $ options, BubbleableMetadata $ bubbleable_metadata) and after

case 'basic_cart_vat':.........break;

insert the following construction

case 'basic_cart_phone': $langcode = $order->language()->getId();                    
if ($order->isTranslatable()) {                        
    $order = $order->getTranslation($langcode);                    
}                    
$phone = $order->get('basic_cart_phone')->getValue();                    
$replacements[$original] = isset($phone[0]['value']) ? $phone[0]['value'] : "";                    
break;

This will allow us to display the token of the standard phone number field, which by default is not displayed in the letter. Insert the token into the letter field

[basic_cart_order:basic_cart_phone]

and he will print out the letter. By analogy, you can display any additional field that you add to the order.

For example, we added an organization name field - with the machine name field_basic_cart_organizaciya. For it, you will need to add a token

case 'field_basic_cart_organizaciya': $langcode = $order->language()->getId();                    
if ($order->isTranslatable()) {                        
    $order = $order->getTranslation($langcode);                    }                   
    $org = $order->get('field_basic_cart_organizaciya')->getValue();                    
    $replacements[$original] = isset($org[0]['value']) ? $org[0]['value'] : "";                   
break;

And in the letter we display the token

[basic_cart_order:field_basic_cart_organization]

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
The comment language code.