Retrieving Cryptocurrency API data in Woocommerce for Price Updates

I want to include Dogecoin into my Woocommerce webshop as the only payment method. Therefore I want to have my prices shown only in Doge instead of USD. I’ve found some solutions but the interaction with the API doesn’t seem to work.

$url = ‘https://min-api.cryptocompare.com/data/price?fsym=DOGE&tsyms=USD’;
$response = wp_remote_get( esc_url_raw( $url ));

if( is_wp_error( $request ) ) {
return false;
}

$body = wp_remote_retrieve_body( $request );
$doge_usd_price = json_decode( $body );

if( !is_admin() )
{
add_filter( ‘woocommerce_get_regular_price’, ‘doge_price’, 10, 2 );
add_filter( ‘woocommerce_get_price’, ‘doge_price’, 10, 2 );

add_filter( ‘woocommerce_currency_symbol’, function( $currency_symbol, $currency ) {
return ‘ DOGE’; //$currency_symbol;
}, 10, 2 );

add_filter( ‘woocommerce_currency’, function( $currency ) {
return $currency;
});
}

function doge_price( $price, $product, $doge_usd_price )
{
$doge_price = $price / $doge_usd_price;
return $doge_price;
}

The issue lies within the last function “doge_price”, if I rewrite it to the function below, it works. How can I take the USD/DOGE price and display the correct amount of DOGE as the price? And is there a possibility to have it live adjusting according to the data from the API?

function doge_price( $price, $product ) {
return $price / 10
}

Read more here:: Retrieving Cryptocurrency API data in Woocommerce for Price Updates

Leave a Reply

Your email address will not be published. Required fields are marked *