Add tokens to the commerce-order token types for commerce-order-total field components base-price shipping etc
Hi,
May be shortterm solution thinking for https://drupal.org/node/2063023
Adding simple tokens based on: http://www.andypangus.com/drupal-7-making-your-own-tokens
I made this custom module but I do not see the tokens showing up in the tokenlist. What do I do wrong please?
Module file:
<?php
/**
* Implements hook_token_info().
*/
function Commerce_Order_Breakup_token_info() {
// Tokens for commerce order totals field.
$order = array();
$order['base-price'] = array(
'name' => t('Order total:Base price'),
'description' => t('The sub-total excluding shipping tax end other fees and charges.'),
);
$order['shipping'] = array(
'name' => t('Order total:Shipping'),
'description' => t('The total cost of shipping.'),
);
$order['tax_rate'] = array(
'name' => t('Order total:Tax'),
'description' => t('The Tax of the order including shipping.'),
);
return array(
'tokens' => array('commerce-order' => $order),
);
}
/**
* Implements hook_tokens().
*/
function Commerce_Order_Breakup_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
if ($type == 'commerce-order' && !empty($data['commerce-order'])) {
$order = $data['commerce-order'];
foreach ($tokens as $name => $original) {
switch ($name) {
// replace commerce order total components tokens with actual values
// from the order.
case 'base-price':
$price_components = commerce_price_component_load($order->commerce_order_total['und']['0'], 'base_price');
// kpr($price_components);
$replacements[$original] = commerce_currency_format($price_components['0']['price']['amount'], $price_components['0']['price']['currency_code']);
break;
case 'shipping':
$price_components = commerce_price_component_load($order->commerce_order_total['und']['0'], 'flat_rate_binnen_nederland');
$replacements[$original] = commerce_currency_format($price_components['0']['price']['amount'], $price_components['0']['price']['currency_code']);
break;
$price_components = commerce_price_component_load($order->commerce_order_total['und']['0'], 'flat_rate_verzenden_buiten_nederland');
$replacements[$original] = commerce_currency_format($price_components['0']['price']['amount'], $price_components['0']['price']['currency_code']);
break;
case 'tax_rate':
$price_components = commerce_price_component_load($order->commerce_order_total['und']['0'], 'btw_hoog');
$replacements[$original] = commerce_currency_format($price_components['0']['price']['amount'], $price_components['0']['price']['currency_code']);
break;
}
}
}
return $replacements;
}
?>
Info file:
name = Commerce Order Breakup tokens
description = A module with the Commerce Order Tokens: Base_price, shipping services and tax rates
package = Commerce Contributed
core = 7.x
dependencies[] = token
files[] = Commerce_Order_Breakup_tokens.module
How can I use these tokens now within my Commerce customer emails?
Thanks a lot in advance for your reply!
Greetings, Martijn
Comments
If you put all the code in a file MYMODULE.tokens.inc. The token module picks up these files automatically.
Your MYMODULE.module file just needs a comment but it is basically empty as follows:
<?php
/**
* @file MYMDOULE.module
* TODO: Enter file description here.
*/
Never end a file with a closing
?>
Also the case shipping try the following as you are only allowed one break the 2nd will never happen:
<?php
$price_components0 = commerce_price_component_load($order->commerce_order_total['und']['0'], 'flat_rate_binnen_nederland');
$price_components1 = commerce_price_component_load($order->commerce_order_total['und']['0'], 'flat_rate_verzenden_buiten_nederland');
$price_components = commerce_price_components_combine($price_components0, $price_components1);
$replacements[$original] = commerce_currency_format($price_components['0']['price']['amount'], $price_components['0']['price']['currency_code']);
break;
Never end a file with a closing
?>
Remove all closing ?>
In the info file no need for:
files[] = Commerce_Order_Breakup_tokens.module