0

I want to change default drupal view of price in all of my field, as a example I want to remove '00' and add my custom postfix to it,

I found hook_commerce_price_field_formatter_prepare_view but when I implement it in my module it not called,really I am not sure this hook is suitable and ca achieve my need??

I just want the all price fields changes to my desire only in front-end view (only in view, because my payment method is according to main currency)

2 Answers 2

3

You can do this using the Commerce Currency API

Implement hook_commerce_currency_info_alter and add your own commerce_currency_format handler

e.g:

/**
 * hook_commerce_currency_info_alter
 * 
 * Add custom currency format to selected currencies
 */
function MY_MODULE_commerce_currency_info_alter(&$currencies){
  $currencies['IRR']['format_callback'] = 'MY_MODULE_commerce_currency_format';
}

/**
 * Currency format callback
 *
 * Remove decimal places from output
 * while keeping commerce_amount_to_decimal formatting the same
 *
 * See: commerce_currency_format
 */
function MY_MODULE_commerce_currency_format($amount, $currency, $object = NULL, $convert = TRUE) {

  // Format the price as a number.
  // In our case, we remove decimal places
  $price = number_format(commerce_currency_round(abs($amount), $currency), 0, $currency['decimal_separator'], $currency['thousands_separator']);

  // Establish the replacement values to format this price for its currency.
  $replacements = array(
    '@code_before' => $currency['code_placement'] == 'before' ? $currency['code'] : '',
    '@symbol_before' => $currency['symbol_placement'] == 'before' ? $currency['symbol'] : '',
    '@price' => $price,
    '@symbol_after' => $currency['symbol_placement'] == 'after' ? $currency['symbol'] : '',
    '@code_after' => $currency['code_placement'] == 'after' ? $currency['code'] : '',
    '@negative' => $amount < 0 ? '-' : '',
    '@symbol_spacer' => $currency['symbol_spacer'],
    '@code_spacer' => $currency['code_spacer'],
  );

  return trim(t('@code_before@code_spacer@negative@symbol_before@price@symbol_spacer@symbol_after@code_spacer@code_after', $replacements));
}
6
  • is this work only in view ?
    – Yuseferi
    Commented Jun 16, 2014 at 6:39
  • our country payment system is with raw price(more zeros), I want to remove some "0" in view, just in view but in payment main value used (with more "0"), It this suitable or it change every where?
    – Yuseferi
    Commented Jun 16, 2014 at 6:55
  • It will work across the site, wherever, the price is displayed. Commented Jun 16, 2014 at 8:46
  • only in display or in back-end operation?
    – Yuseferi
    Commented Jun 16, 2014 at 9:27
  • Both the front-end and back-end display will use the new format callback. Note: the raw price value is still stored in the database, the format callback is only used for display. Commented Jun 16, 2014 at 9:40
1

I'm gonning to complete what David Thomas wrote. 1/ Create a custom module with the code. Replace My_Module by the name of your module.

/**
 * hook_commerce_currency_info_alter
 * 
 * Add custom currency format to selected currencies
 */
function MY_MODULE_commerce_currency_info_alter(&$currencies){
  $currencies['IRR']['format_callback'] = 'MY_MODULE_commerce_currency_format';
}

/**
 * Currency format callback
 *
 * Remove decimal places from output
 * while keeping commerce_amount_to_decimal formatting the same
 *
 * See: commerce_currency_format
 */
function MY_MODULE_commerce_currency_format($amount, $currency, $object = NULL, $convert = TRUE) {

  // Format the price as a number.
  // In our case, we remove decimal places
  $price = number_format(commerce_currency_round(abs($amount), $currency), 0, $currency['decimal_separator'], $currency['thousands_separator']);

  // Establish the replacement values to format this price for its currency.
  $replacements = array(
    '@code_before' => $currency['code_placement'] == 'before' ? $currency['code'] : '',
    '@symbol_before' => $currency['symbol_placement'] == 'before' ? $currency['symbol'] : '',
    '@price' => $price,
    '@symbol_after' => $currency['symbol_placement'] == 'after' ? $currency['symbol'] : '',
    '@code_after' => $currency['code_placement'] == 'after' ? $currency['code'] : '',
    '@negative' => $amount  $currency['symbol_spacer'],
    '@code_spacer' => $currency['code_spacer'],
  );

  return trim(t('@code_before@code_spacer@negative@symbol_before@price@symbol_spacer@symbol_after@code_spacer@code_after', $replacements));
}

2/ Go to Custom Currency Setting (admin/commerce/config/currency/custom-settings) and setup the field Format Callback with: My_Module_ommerce_currency_format

At this point your node will display what you asked, but not your views (or your fields).

3/ To use this setting into your views display, you have to display the node inside your views. If you display a field it won't work. Views > Show: Content

Cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.