这个想法非常简单,因为 URL 会自动更新价格本身,我所需要做的就是在括号内添加 BTC 价格。
示例:
0.00003573
是使用此 URL 的结果:
https://www.blockchain.com/tobtc?currency=USD&value=1
但是,出了点问题,我不知道如何解决它。我得到“
Warning: A non-numeric value
”。我需要根据产品价格显示 URL 的输出。
这是我正在使用的代码:
function manual_currency_conversion( $price ) {
$btc_conversion_rate = "https://www.blockchain.com/tobtc?currency=USD&value=1"; // returns this: 0.00003573 = which will auto update automatically
$btc_price = (float) $price / $btc_conversion_rate;
return number_format( $btc_price, 8, '.', '' );
}
add_filter( 'wc_price', 'btc_additional_currency', 10, 5 );
function btc_additional_currency( $return, $price, $args, $unformatted_price, $original_price = null ) {
$display_btc_price = manual_currency_conversion( $price );
$btc_currency = '₿';
$currency_symbol = $btc_currency;
$display_btc_price = $currency_symbol . $display_btc_price;
$btc_price_on_product_page = '<small><span class="btc-price"> ( ' . $display_btc_price . ' ) </small></span>';
return $return . $btc_price_on_product_page;
}
file_get_contents()
PHP 函数 和您的 URL 来有效获取所需的数据。
我修改并简化了您的代码:
function get_btc_price( $price ) {
$btc_rate = file_get_contents("https://www.blockchain.com/tobtc?currency=USD&value=1");
return number_format( floatval($price) * floatval($btc_rate), 8, '.', '' );
}
add_filter( 'wc_price', 'btc_additional_currency', 10, 2 );
function btc_additional_currency( $price_html, $price ) {
return $price_html . '<small class="btc-price"> ( ₿'. get_btc_price( $price ). ' )</small>';
}