1
Answers
Vote up!
1
Vote down!

Round Display Price with Condition

Hi,

I'm using Drupal 7.22, Drupal Commerce 7.x-1.7 and Commerce Multicurrency 7.x-1.3.

I have two currency, IDR and USD. I want to round my display price (in views and node) but with condition original currency code = USD.

so far i have created custom module. just like below

<?php
/**
* Implements hook_commerce_currency_info_alter().
*
*Fix IDR front placement
*/
function commerce_idr_commerce_currency_info_alter(&$currencies, $langcode) {
$currencies['IDR']['symbol'] = 'Rp ';
$currencies['IDR']['symbol_placement'] = 'before';
$currencies['IDR']['code_placement'] = '';

$currencies['IDR']['format_callback'] = 'commerce_idr_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 commerce_idr_commerce_currency_format($amount, $currency, $object = NULL, $convert = TRUE) {

if($currency_code=='USD') {
$amount = round($amount,-4);
}

// 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));
}

with dpm($content); i found $...['product:commerce_price']['#object']->commerce_price['und'][0]['original']['currency_code']

but i can't check USD in my custom module. I've tried something like this line
if($currency_code=='USD') {
$amount = round($amount,-4);
}

but nothing happen.

Please give me some clues to check the original currency code within custom module. How can i find the right variable to check

Regards

mbahlol

Asked by: mbahlol
on July 15, 2013

1 Answer

Vote up!
0
Vote down!

After some searching and trying. I found a solution for me. Using devel module dpm($object); on function commerce_idr_commerce_currency_format I found the right array. With this I can round $amount just like I want.

if($object->commerce_price['und'][0]['original']['currency_code']=='USD') {
$amount = round($amount,-4);
}

I hope this is the right solution.

regards

mbahlol

Answer by: mbahlol
Posted: Jul 20, 2013