1

Currently I'm using Commerce and Commerce Multicurrency to our currency in Indonesia.

Sometimes there is a price to numbers that are not rounded.

With print render ($ content ['product: commerce_price']) I get the original price.

I've tried to edit the tpl.php file and the results are as desired.

number_format (round ($ price, -4), 0);

2,511,000 into 2,500,000 2,565,000 into 2,600,000

But after some reading and hopefully not misread. That is not good in tpl.php, but should be in template.php or modules.

Please advise to help me.

1
  • after searching all night, and I found the answer by creating a custom module based on [this][1] function bulat($bilangan){ $bulat = number_format(round($bilangan,-4), 0); return $bulat; } and print it in my node.tpl.php. I hope the way I use it correctly and safely [1]: drupal.org/node/81681#comment-150049
    – just-d
    Commented Jun 16, 2013 at 2:03

2 Answers 2

1

A better way to adjust currency formatting, using the relevant Commerce currency hooks, is with

hook_commerce_currency_info_alter

and adding 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['IDR']['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
  • thanks @david-thomas for your advice. right now i still trying THEME_preprocess_node(&$variables) after this i will try yours. because i'm still stuck
    – just-d
    Commented Jun 17, 2013 at 9:53
  • Preprocess node will only affect node output, you should use the above method to ensure the currency is formatted correctly in all places where currency is output, e.g order admin line items etc... Commented Jun 17, 2013 at 10:14
  • @DavidThomas Though you have voted negatively to my answer & have written great solution for this, but as per question asked by him I answered correctly & I don't see any reason behind negative vote...
    – RajeevK
    Commented Jun 17, 2013 at 16:20
  • @DavidThomas I'm trying the solution you provide. I managed to modify prices and price variations were able to take changed. thank you very much for the solution. now I'm trying to learn more deeply. I'm very interested in learning more deeply, where should I start reading something like this?
    – just-d
    Commented Jun 18, 2013 at 0:07
  • @mbahol, perhaps start with reading the API documentation at: api.drupalcommerce.org Once you know what hooks are available, the best approach becomes much clearer. Commented Jun 18, 2013 at 3:09
0

As per your comment you have done it by making some module. But still I will tell you my way to doing it.

As you want to print the value after rounding it in your node.tpl.php, you can go for implementing THEME_preprocess_node(&$variables) function in your template.php file of the theme which you are using. Replace THEME with your theme name in there.

Now implement something like this,

function indiareads_preprocess_node(&$vars){
  if ($node = menu_get_object()){
    if($node->type == 'YOUR_PRODUCT_DISPLAY_TYPE'){
      $product_id = $node->FIELD_PRODUCT_REFERENCE['und'][0]['product_id'];
      $product = commerce_product_load($product_id);
      $price = $product->commerce_price['und'][0]['amount'];
      $rounded_price = number_format(round($price,-4), 0);
      $vars['rounded_price'] = $rounded_price;
    }
  }
}

Replace YOUR_PRODUCT_DISPLAY_TYPE with the name of content type which is displaying your product & FIELD_PRODUCT_REFERENCE with the name of field of that content type which reference the commerce product.

After this when you will go to node.tpl.php, you will have $rounded_price variable available to print where ever you want.

I am writing this for better understanding of the system even if you have done that, so that if somebody comes searching can get help.

4
  • thanks @RajeevK. right now i'm trying your solution. but still stuck in render and only work with USD price.
    – just-d
    Commented Jun 17, 2013 at 9:48
  • @mbahlol - Try a bit more & you will find solution :)
    – RajeevK
    Commented Jun 17, 2013 at 9:53
  • This will only affect node output, resulting in inconsistent pricing display throughout the site. Commented Jun 17, 2013 at 10:29
  • @DavidThomas I have just given the example for the sake of understanding to do something like this from template.php, which he was asking. And if I have particular node display type & I need to show the price at some place in some format, this is the way I follow...
    – RajeevK
    Commented Jun 17, 2013 at 11:06

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.