Met Drupal commerce kan gemakkelijk een webshop gemaakt worden. Een van de mogelijkheden is dat er bevestigingsemails verzonden worden zodra er een bestelling geplaatst is. Drupal commerce regelt dit met Regels (Rules). Met behulp van tokens kan de inhoud van de emails door een beheerder ingesteld worden.
De tokens [commerce-order:commerce_customer_billing] en [commerce-order:commerce_customer_shipping] worden gebruikt om de gegevens van het factuur- en het verzendadres in de email op te nemen. De losse adresgegevens worden echter allemaal achter elkaar op één regel geplaatst.
Om meer controle te hebben over welke informatie er getoond wordt kun je ook zelf tokens definieren. In onderstaande code worden drie nieuwe tokens gedefinieerd. De eerst twee tokens zijn de vervanging voor de factuur- en de verzendadres tokens. De derde toont productinformatie.
<?php
/**
* Implements hook_token_info_alter().
* Define the tokens.
*/
function mymodule_commerce_tokens_extra_token_info_alter(&$info) {
$info['tokens']['commerce-order']['billing-details'] = array(
'name' => t('Billing Details token'),
'description' => t('Custom billing details token.'),
);
$info['tokens']['commerce-order']['shipping-details'] = array(
'name' => t('Shipping Details token'),
'description' => t('Custom shipping details token.'),
);
$info['tokens']['commerce-order']['product-details'] = array(
'name' => t('Product Details token'),
'description' => t('Custom product details token.'),
);
}
/**
* Implements hook_tokens().
* Translates the tokens to their values.
*/
function mymodule_commerce_tokens_extra_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
if ($type == 'commerce-order' && !empty($data['commerce-order'])) {
// Get order details.
$order = $data['commerce-order'];
$total = $order->commerce_order_total[LANGUAGE_NONE][0]['currency_code'] . ' ' .
number_format((int)$order->commerce_order_total[LANGUAGE_NONE][0]['amount']/100, 2, ',', '.');
foreach ($order->commerce_line_items[LANGUAGE_NONE] as $line_item) {
$items[] = commerce_line_item_load($line_item['line_item_id']);
}
// add output to tokens
foreach ($tokens as $name => $original) {
switch ($name) {
case 'billing-details':
// Get profile details.
$profile_id = $order->commerce_customer_billing[LANGUAGE_NONE][0]['profile_id'];
$profile = commerce_customer_profile_load($profile_id);
// Get the customer details.
$customer = $profile->commerce_customer_address[LANGUAGE_NONE][0];
// Get the fields from the billing address and a custom field as well.
$replacements[$original] = mymodule_commerce_tokens_extra_address($profile, $customer);
break;
case 'shipping-details':
// Get profile details.
$profile_id = $order->commerce_customer_billing[LANGUAGE_NONE][0]['profile_id'];
$profile = commerce_customer_profile_load($profile_id);
// Get the customer details.
$customer = $profile->commerce_customer_address[LANGUAGE_NONE][0];
// Get the fields from the shipping address and a custom field as well.
$replacements[$original] = mymodule_commerce_tokens_extra_address($profile, $customer);
break;
case 'product-details':
$output = '';
foreach ($items as $item) {
$output .= number_format($item->quantity) . ' ' . $item->line_item_label . ' - ' .
$item->commerce_total[LANGUAGE_NONE][0]['currency_code'] . ' ' .
number_format($item->commerce_total[LANGUAGE_NONE][0]['amount']/100, 2, ',', '.') . "\n";
}
$replacements[$original] = $output . "\n" . t('Order total:') . ' ' . $total;
break;
}
}
}
return $replacements;
}
/**
* Get the information from the address entity.
*/
function mymodule_commerce_tokens_extra_address($profile, $customer) {
// Get all country codes and names.
$countries = country_get_list();
$return = $customer['name_line'] . "\n" . $customer['thoroughfare'] . " " . $customer['premise'] . "\n" .
$customer['postal_code'] . ' ' . $customer['locality'] . "\n" .
$countries[$customer['country']] . "\n" .
$profile->field_phone[LANGUAGE_NONE][0]['safe_value'];
return $return;
}