Needs work
Project:
Drupal Commerce Extra Price Formatters
Version:
7.x-1.x-dev
Component:
Code
Priority:
Minor
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
1 Aug 2012 at 16:32 UTC
Updated:
1 Aug 2012 at 16:32 UTC
I needed a price formatter that will display the price like:
€ 45,00 including 19% VAT, excluding shipping
or in case of 6% VAT:
€ 5,00 including 6% VAT, excluding shipping.
Unfortunatly I couldn't figure it out how to do this with this module, so based on your module I created the following, maybe you can implement it and make it better.
function MYMODULE_field_formatter_info() {
return array(
'commerce_price_prefix_suffix_vat' => array(
'label' => t('Price with Prefix / Suffix and VAT'),
'field types' => array('commerce_price'),
'settings' => array(
'prefix' => '',
'suffix' => '',
'text_format' => NULL,
'calculation' => TRUE,
'show_vat' => TRUE,
),
),
);
}
function MYMODULE_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$element = array();
switch ($display['type']) {
case 'commerce_price_prefix_suffix_vat':
$element['calculation'] = array(
'#type' => 'checkbox',
'#value' => TRUE,
'#title' => t('Show calculated price'),
'#default_value' => empty($settings['calculation']) ? 'TRUE' : $settings['calculation'],
);
$element['prefix'] = array(
'#type' => 'textfield',
'#title' => t('Prefix'),
'#description' => t('Text will be shown before the price'),
'#default_value' => empty($settings['prefix']) ? '' : $settings['prefix'],
);
$element['suffix'] = array(
'#type' => 'textfield',
'#title' => t('Suffix'),
'#description' => t('Text will be shown after the price'),
'#default_value' => empty($settings['suffix']) ? '' : $settings['suffix'],
);
$element['show_vat'] = array(
'#type' => 'checkbox',
'#value' => TRUE,
'#title' => t('Show including or excluding VAT rate'),
'#description' => t('Text will be shown after the price'),
'#default_value' => empty($settings['show_vat']) ? 'TRUE' : $settings['show_vat'],
);
// Check which text filters the current user is permitted to use.
global $_MYMODULE_account;
$filter_options = array();
foreach (filter_formats($_MYMODULE_account) as $key => $values) {
$filter_options[$key] = $values->name;
}
$element['text_format'] = array(
'#type' => 'select',
'#title' => t('Text Format'),
'#options' => $filter_options,
'#description' => t('The text format filter that will be applied. The same filter will be applied to prefix and suffix.'),
'#default_value' => empty($settings['text_format']) ? '' : $settings['text_format'],
);
break;
}
return $element;
}
function MYMODULE_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$summary = array();
switch ($display['type']) {
case 'commerce_price_prefix_suffix_vat':
$text_format = empty($display['settings']['text_format']) ? NULL : $display['settings']['text_format'];
if ($settings['calculation'] == TRUE) {
$summary[] = t('Show calculated price with prefix <em>') . check_markup($settings['prefix'], $text_format) . t('</em> and suffix <em>') . check_markup($settings['suffix'], 'filtered_html') . '</em>';
}
else {
$summary[] = t('Show base price with prefix <em>') . check_markup($settings['prefix'], $text_format) . t('</em> and suffix <em>') . check_markup($settings['suffix'], $text_format) . '</em>';
}
break;
}
return implode('<br />', $summary);
}
function MYMODULE_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) {
// TODO: Loop over the instances and pass them to this hook individually so we
// can enforce prices displaying with components as not being altered.
// Allow other modules to prepare the item values prior to formatting.
foreach (module_implements('commerce_price_field_formatter_prepare_view') as $module) {
$function = $module . '_commerce_price_field_formatter_prepare_view';
$function($entity_type, $entities, $field, $instances, $langcode, $items, $displays);
}
}
function MYMODULE_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
// Loop through each price value in this field.
foreach ($items as $delta => $item) {
// Theme the display of the price based on the display type.
switch ($display['type']) {
case 'commerce_price_prefix_suffix_vat':
$markup = '';
$markuptekst ='';
$text_format = empty($display['settings']['text_format']) ? NULL : $display['settings']['text_format'];
if (!empty($display['settings']['prefix'])) {
$markup .= '<span class="price_prefix">' . check_markup($display['settings']['prefix'], $text_format) . '</span> ';
}
$markup .= commerce_currency_format($item['amount'], $item['currency_code'], $entity);
if (!empty($display['settings']['suffix']) || $display['settings']['show_vat']) {
$markup .= ' <span class="price_suffix">';
if($display['settings']['show_vat']) {
$markuptekst .= MYMODULE_format_vat($item);
}
if(!empty($display['settings']['suffix'])) {
$markuptekst .= $display['settings']['suffix'];
}
$markup .= check_markup($markuptekst, $text_format) . '</span>';
}
$element[$delta] = array(
'#markup' => $markup,
);
break;
}
}
return $element;
}
function MYMODULE_format_vat($price) {
$taxtext = t('Excluding VAT') ;
$price_vat = $price;
//load list of vat rates
$vatrates = commerce_tax_rates();
//itterate trough list:
foreach ($vatrates as $name => $vatinfo) {
// Get taxcomponent
$name = 'tax|'.$name;
$tax_component = commerce_price_component_load($price_vat, $name);
$rate = $vatinfo['rate'] * 100;
if($rate > 0) {
if(!empty($tax_component)) {
$taxtext = t('Including !rate% VAT', array('!rate' => $rate, )) ;
}
}
}
return $taxtext;
}