Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
In .module:
/**
* Provide information about our custom placeholder/token.
*
* @see httx://api.drupal.org/api/drupal/modules--system--system.api.php/function/hook_token_info/7
* @see httx://api.lullabot.com/token_example_token_info/7
* @return array
* An associative array of available tokens and token types.
*/
function commerce_bank_transfer_token_info() {
$info['tokens']['commerce-order']['order-payment-bank-details'] = array(
'name' => t('Order payment bank details'),
'description' => t('Token for details of bank transfer'),
);
return $info;
}
/**
* Provide replacement values for placeholder tokens.
*
* @see httx://api.drupal.org/api/drupal/modules--system--system.api.php/function/hook_tokens/7
* @see httx://api.lullabot.com/token_example_tokens/7
* @param string $type
* The machine-readable name of the type (group) of token being replaced, such
* as 'node', 'user', or another type defined by a hook_token_info()
* implementation.
* @param array $tokens
* An array of tokens to be replaced. The keys are the machine-readable token
* names, and the values are the raw [type:token] strings that appeared in the
* original text.
* @param array $data (optional)
* An associative array of data objects to be used when generating replacement
* values, as supplied in the $data parameter to token_replace().
* @param array $options (optional)
* An associative array of options for token replacement; see token_replace()
* for possible values.
* @return array
* An associative array of replacement values, keyed by the raw [type:token]
* strings from the original text.
*/
function commerce_bank_transfer_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
$sanitize = !empty($options['sanitize']);
if ($type == 'commerce-order' && !empty($data['commerce-order'])) {
$order = $data['commerce-order'];
foreach ($tokens as $name => $original) {
switch ($name) {
// Simple key values on the order.
case 'order-payment-bank-details':
$payment_method = $order->data['payment_method'];
list($method_id, $rule_name) = explode('|', $payment_method);
if($method_id == 'bank_transfer') {
$instance_id = $method_id . '|' . "commerce_payment_" . $method_id;
$bank_transfer = commerce_payment_method_instance_load($instance_id);
$replacements[$original] = $bank_transfer['settings']['policy'];
}
else {
$replacements[$original] = '';
}
break;
}
}
}
return $replacements;
}
Then you can use [commerce-order:order-payment-bank-details] in email template.
Comments
Comment #0.0
Carlitus CreditAttribution: Carlitus commenteda minor change
Comment #1
rszrama CreditAttribution: rszrama at Centarro commented