Adding a Custom Token to the Ubercart (before version 2.3) Email Templates
This tutorial is for Ubercart versions earlier than version 2.3. There is a newer version of this example for Ubercart 2.3 or later.
In this post I’ll show how to set up an extra token for use in an Ubercart template. This will require creating a custom module (because we don’t really want to modify others’ modules). We’ll use a few different hooks to create the token, and then simply modify the template to include this token. This setup requires the token module (which is required by Ubercart), so make sure this is enabled!
Before diving into this, please make sure you have a Ubercart system that sends emails when orders are placed. You should also have a test account from which you can place orders and receive emails.
2) Create the Ubercart Template
You can find the template files in the ubercart/uc_orders/templates directory. You must make a copy of the customer template and name it something unique (must end with .itpl.php). After doing so, you can choose this new template in Ubercart by going to admin/store/settings/orders/edit. The drop down there should show your new template. This setting is for the on-site invoice template viewing within the Drupal site. To set this template as the email template, you must also edit the conditional action by going to admin/store/ca/uc_checkout_customer_notification/edit/actions. You will find the setting under the fieldset “Action: Email an order invoice.”
3) Add a hook_token_values() to your custom module
This code creates a token containing the last 4 digits of the credit card number used for an Ubercart order. I’ll leave it up to you to research any of the code used. This is simply an example. You could add variables from any number of sources, including anything available in the $order object. Anything you can print to the screen you should be able to add as a token. Note: you should probably make sure the string is email-ready.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
/** * Implementation of hook_token_values(). (token.module) */ function mymodule_token_values($type, $object = NULL) { $values = array(); switch ($type) { case 'order': $order = $object; if ($order->payment_details['cc_number']) { $values['order-cc-number'] = uc_credit_display_number($order->payment_details['cc_number']); } break; } return $values; } |
4) Add a hook_token_list() to show token in list of tokens
1 2 3 4 5 6 7 8 9 10 |
/** * Implementation of hook_token_list(). (token.module) */ function mymodule_token_list($type = 'all') { if ($type == 'order' || $type == 'ubercart' || $type == 'all') { $tokens['order']['order-cc-number'] = t('The last 4 digits of the credit card number used for the order.'); } return $tokens; } |
You can now see your new token in the list of available ubercart tokens (admin/store/help/tokens)
5) Add the token to your template
Here is a snippet that will show the new token:
1 2 3 4 5 6 7 8 |
<tr> <td nowrap="nowrap"> <b><?php echo t('Credit Card Details'); ?></b> </td> <td width="98%"> [order-cc-number] </td> </tr> |
6) Clear the Drupal cache and make sure your custom module is enabled.
7) Create an order using an email account you can check. Then, verify that it is working as expected (check the email that is sent, as well as the online invoice).
Please check out the Token and Ubercart APIs for more information! If you have problems, make sure your module doesn’t have any errors, and make sure it’s being used. Also, verify that your Ubercart system is using the new template!