You are here

function commerce_gc_tokens in Commerce GC 7

File

./commerce_gc.tokens.inc, line 25
Provides token support for Giftcards.

Code

function commerce_gc_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();
  foreach ($tokens as $name => $original) {

    // Add a token for giftcard balance.
    if ($type == 'commerce-coupon' && $name == 'giftcard-balance' && !empty($data['commerce-coupon'])) {
      $coupon = $data['commerce-coupon'];
      $amount = commerce_gc_giftcard_balance($coupon->coupon_id);
      $replacements[$original] = commerce_currency_format($amount, commerce_default_currency());
    }

    // Add giftcards url token.
    if ($type == 'user' && $name == 'giftcards-url') {
      $url_options = array(
        'absolute' => TRUE,
      );
      $user = $data['user'];
      $replacements[$original] = $user->uid ? url('user/' . $user->uid . '/giftcards', $url_options) : '';
    }
  }
  return $replacements;
}