Adding a Custom Token to the Ubercart Email Template(s)

Note: this post is a direct copy and paste from Agile Adam’s post. since that page started throwing a 404. Google’s cache still had it so I grabbed it and posted it here so it wouldn’t be lost forever. If the page comes back up or if Adam gets mad, holler and I’ll take this down.

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.

1) Create a custom module

Just an empty modulename.module and modulename.info will do for now.

2) Create the Ubercart Template

Template files are in ubercart/uc_orders/templates. 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 in the Order Settings page. 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 in the Ubercart settings.

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.

/**
 * 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

/**
 * 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

Add your token using the following in your template:

[order-cc-number]

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!

Posted 01:50 PM | 0 Comments | Tags:

Comments

WTF?