Rp 500000 (USD $ 37.49)
I want to show two currency looks like above in Cart Totals in woocommerce (subtotal, shipping, discount, insurance[custom fee], packing fee[custom fee]).
I succed make it on subtotal, shipping, discount with adding below filter.
But that doesn't work on the two custom fee (insurance, packing fee). When I add $price_us to the .woocommerce-Price-amount.amount class, the insurance/packing fee amount will going wrong. If I did not add $price_us to the .woocommerce-Price-amount.amount class everything will be ok, but with only 1 currency.
function my_custom_price_format( $formatted_price, $price, $decimals,
$decimal_separator, $thousand_separator ) {
$price_us_int = intval(preg_replace('/[^0-9]+/', '', $price), 10);
$price_us_int = convert_idr_to_usd_cart($price_us_int);
$price_us = "USD $ $price_us_int";
return '<span class="woocommerce-Price-amount amount">' . $formatted_price .' ( '.$price_us.' )</span>';
}
add_filter( 'formatted_woocommerce_price', 'my_custom_price_format', 20, 5 );
Is there anybody could help me with this situation.
Update
There is 2 main problems:
1) You are using the wrong hook. Instead you should use wc_price
.
2) In your code and it's because you should directly use the unformatted $price
argument available without using this:
$price_us_int = intval(preg_replace('/[^0-9]+/', '', $price), 10);
Now as I don't have the code of your custom function convert_idr_to_usd_cart()
I don't now how you make your calculation and how you set the number of decimals or this converted custom price.
So I have use this for testing purpose:
// Just for testing
function convert_idr_to_usd_cart( $price ){
$convertion_rate = 0.016;
$new_price = $price * $convertion_rate;
return number_format($new_price, 2, '.', '');
}
Here is the correct functional code (without this issues):
add_filter( 'wc_price', 'my_custom_price_format', 10, 3 );
function my_custom_price_format( $formatted_price, $price, $args ) {
// The currency conversion custom calculation function
$price_usd = convert_idr_to_usd_cart($price);
// the currency symbol for US dollars
$currency = 'USD';
$currency_symbol = get_woocommerce_currency_symbol( $currency );
$price_usd = $currency_symbol.$price_usd; // adding currency symbol
// The USD formatted price
$formatted_price_usd = "<span class='price-usd'> (USD $price_usd)</span>";
// Return both formatted currencies
return $formatted_price . $formatted_price_usd;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works.