【发布时间】:2017-12-12 04:01:18
【问题描述】:
我正在尝试显示多种货币,
就像我们将在后端以美元给出价格,但它将转换为比特币作为主要价格,然后是美元价格,然后是欧元价格,这是我正在使用的代码......
function convertCurrency($amount, $from, $to){
$data = file_get_contents("https://finance.google.com/finance/converter?a=$amount&from=$from&to=$to");
preg_match("/<span class=bld>(.*)<\/span>/",$data, $converted);
$converted = preg_replace("/[^0-9.]/", "", $converted[1]);
return number_format(round($converted, 3),2);
}
add_filter( 'wc_price', 'my_custom_price_format', 10, 4 );
function my_custom_price_format( $formatted_price, $price, $args, $unformatted_price ) {
$price_eur = convertCurrency($price, 'USD','EUR');
$formatted_price_eur = "<br><span class='price-eur'> (€$price_eur)</span>";
$rate_source = 'CoinDesk';
// The currency conversion custom calculation function
$price_btc = $value = WCR_Bitcoin_Exchange_Rate::get( $price, 'USD', 'BTC', $rate_source );
// the currency symbol for US dollars
$currency_symbol = '<i class="fa fa-btc"></i> ';
$price_btc = $currency_symbol.$price_btc; // adding currency symbol
//Bitcoin formattd price
$formatted_price_btc = "<br><span class='price-btc'> $price_btc</span>";
// The USD formatted price
$formatted_price = '<br>('.$formatted_price .')';
// Return both formatted currencies
return $formatted_price_btc . $formatted_price . $formatted_price_eur ;
}
但我得到的只是美元价格,但其他价格为零。当我回显 $price 时,它会显示双倍价格,请参见截图:
请让我知道哪里做错了..
(有关信息:WCR_Bitcoin_Exchange_Rate::get() 已经过测试并可以使用)
【问题讨论】:
标签: php wordpress woocommerce currency cart