0

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 &$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']) && $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?

1

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 &$replacements, array $context) {
  if ($context['type'] == 'commerce-order') {
    if (!empty($replacements['payment-method']) && $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 &$replacements, array $context) {
  if ($context['type'] == 'commerce-order') {
    if (!empty($replacements['payment-method-title']) && $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).

  • Thanks for your answer. Looks like it should work, but the token value still is "card_payment" in the email sent after an order is placed and payed. I also added "return $replacements" on the last row, with no luck. – TBJ Oct 16 at 6:11
  • The return $replacements; line is not necessary, as $replacements is passed by reference. You should check if that the module is installed and enabled, that token is really a Drupal token, how the token is replaced in the email, and if there are other modules altering the tokens that could change back that token value. As you posted the code you are using, I pointed out what is wrong in that code. – kiamlaluno Oct 16 at 8:22
  • I only meant that when the code didn't work I tried to add that line. I understand now that is not necessary. It still doesn't work though. I will keep trying. Thanks very much for the input. – TBJ Oct 16 at 8:51
0

I am sure the answer provided by kiamlaluno is correct, and I don't know why it doesn't work for me. Maybe it does work and another commerce module change the value back again, but in case anyone else is struggling with similar problems I would like to share a very simple workaround.

The payment method form is created with the hook hook_commerce_payment_method_info(). As an option to this hook it is possible to use an extra field called display_title, which will be used to display the form's title to users. If this field is not specified it defaults to the title field. More info here.

So, in this case the field title becomes free to use for other purposes and as this field's value also is available "out of the box" as a replacement token ([commerce-order:payment-method-title]) in the admin/commerce/config/email page, I used this field in my email, which worked straight away.

/**
 * Implements hook_commerce_payment_method_info().
 */
function invoice_payments_commerce_payment_method_info() {
  $payment_method = array();

  $payment_method['card_payment'] = array(
    'base' => 'card_payment',
    'weight' => 0,
    'title' => t('This order is paid'),
    'display_title' => t('Card payment'),
    'description' => t('Pay your order with card.'),
    'active' => TRUE,
    'offsite' => FALSE,
    'offsite_autoredirect' => FALSE,
  );

  return $payment_method;
}

And then in the Order email configuration page:

En ny beställning har gjorts på hemsidan.

Payment:

[commerce-order:payment-method-title] // will show "This order is paid" when this payment method is used
etc....
  • The code shown in the question alters the [commerce-order:payment] token, while in this answer you are saying that you are using the [commerce-order:payment-method-title] token, which is a different token. That explains why the code I suggest doesn't give the expected result. – kiamlaluno Oct 16 at 12:47

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.