Search code examples
woocommercecurrencydata-conversionbitcoin

Automatic Currency Conversion From USD to BTC Using blockchain.com URL


The idea is very simple since the URL automatically updates the price itself, all I need is to add the BTC price within brackets.

Example: 0.00003573 is the result from using this URL:

https://www.blockchain.com/tobtc?currency=USD&value=1

But, something is wrong and I am not sure how to fix it. I get "Warning: A non-numeric value". I need to display the output from the URL based on the product price.

This is the code I am working with:

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

Solution

  • Updated (Also allow other currencies and default currency is taken from settings)

    To avoid this issue, you need to use file_get_contents() PHP function with your URL to get effectively the desired data.

    Also, the rate needs to be multiplied with the price to get the correct converted price (instead of a division).

    I have revised and simplified your code:

    function get_btc_price_from( $price, $currency_from = '' ) {
        $currency_from = empty( $currency_from ) ? get_option('woocommerce_currency') : $currency_from;
        $btc_rate = file_get_contents("https://www.blockchain.com/tobtc?currency={$currency_from}&value=1");
        return number_format( floatval($btc_rate) * floatval($price), 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_from( $price, 'USD' ). ' )</small>';
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). Tested and works.


    Example usage with EUROS:

    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_from( $price, 'EUR' ). ' )</small>';
    }
    

    Example usage with Default WooCommerce currency:

    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_from( $price ). ' )</small>';
    }