php - Calculations displaying 2 Currencies in WooCommerce Cart prices -
rp 500000 (usd $ 37.49)
i want show 2 currency looks above in cart totals in woocommerce (subtotal, shipping, discount, insurance[custom fee], packing fee[custom fee]).
i succed make on subtotal, shipping, discount adding below filter.
but doesn't work on 2 custom fee (insurance, packing fee). when add $price_us .woocommerce-price-amount.amount class, insurance/packing fee amount going wrong. if did not add $price_us .woocommerce-price-amount.amount class ok, 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 me situation.
update
there 2 main problems:
1) using wrong hook. instead should use wc_price
.
2) in code , it's because should directly use unformatted $price
argument available without using this:
$price_us_int = intval(preg_replace('/[^0-9]+/', '', $price), 10);
now don't have code of custom function convert_idr_to_usd_cart()
don't how make calculation , how set number of decimals or converted custom price.
so have use for testing purpose:
// testing function convert_idr_to_usd_cart( $price ){ $convertion_rate = 0.016; $new_price = $price * $convertion_rate; return number_format($new_price, 2, '.', ''); }
here correct functional code (without issues):
add_filter( 'wc_price', 'my_custom_price_format', 10, 3 ); function my_custom_price_format( $formatted_price, $price, $args ) { // currency conversion custom calculation function $price_usd = convert_idr_to_usd_cart($price); // currency symbol dollars $currency = 'usd'; $currency_symbol = get_woocommerce_currency_symbol( $currency ); $price_usd = $currency_symbol.$price_usd; // adding currency symbol // 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 active child theme (or theme) or in plugin file.
this code tested , works.
Comments
Post a Comment