Order tokens in Basic Cart 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 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_org': $langcode = $order->language()->getId();                    
if ($order->isTranslatable()) {                        
    $order = $order->getTranslation($langcode);                    }                   
    $org = $order->get('field_basic_cart_org')->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]

And lower, in function basic_cart_token_info() we need to add our new tokens:

$info['tokens']['basic_cart_order']['basic_cart_phone'] = [
    'name' => t('Basic cart phone'),
    'description' => t('Phone defined with the order.'),
  ];
$info['tokens']['basic_cart_order']['field_basic_cart_org'] = [
      'name' => t('Basic cart org name'),
      'description' => t('Organization name for order.'),
  ];


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *