How to change the value of a commerce token? When an order is submitted on our D7 site an email is sent to us with all the details of the order, including the payment method.

I use the token [commerce-order:payment-method] in the commerce email configuration page , which works fine, but I would like to change the value of that token to something that is more readable, like "This order is paid".

This is what I have so far in a custom module:

    function mymodule_tokens_alter(array &amp;$replacements, array $context) {

  if ($context['type'] == 'commerce-order') {
    // Find token starting with commerce-order:payment-method.
    if ($value_tokens = token_find_with_prefix($context['tokens'], 'payment-method')) {
      if (!empty($value_tokens['payment-method']) &amp;&amp; $value_tokens['payment-method'] === 'card_payment') {
        $replacements[$value_tokens['payment-method']] = 'This order is paid';
      }
    }
  }
}


The token's value still is card_payment, when the email arrives, so I have probably misunderstood something. Anyone?
token_find_with_prefix() doesn't return, in your case, the [commerce-order:payment-method] token, but tokens like [commerce-order:payment-method:date] or [commerce-order:payment-method:client], if they exist.

The correct code you should use is the following one.

function mymodule_tokens_alter(array &amp;$replacements, array $context) {
  if ($context['type'] == 'commerce-order') {
    if (!empty($replacements['payment-method']) &amp;&amp; $replacements['payment-method'] == 'card_payment') {
      $replacements['payment-method'] = 'This order is paid';
    }
  }
}


As side note, altering the value returned for the [commerce-order:payment-method] token doesn't alter the value returned for similar tokens like [commerce-order:payment-method-title]. If you need to alter the value returned for [commerce-order:payment-method-title], the code needs to be similar to the following one. 

function mymodule_tokens_alter(array &amp;$replacements, array $context) {
  if ($context['type'] == 'commerce-order') {
    if (!empty($replacements['payment-method-title']) &amp;&amp; $replacements['payment-method-title'] == 'the payment title you want to change') {
      $replacements['payment-method-title'] = 'This order is paid';
    }
  }
}


Remember to change 'the payment title you want to change' with the effective payment title you need to alter, which probably isn't 'card_payment' (but that is something you need to verify).