Woocommerce price to sats

Series of php snippets to transform the price of your woocommerce shop in bitcoin or satoshis. Github repo

Notice, these snippets only change the price to its equivalent in btc/sats, if you want to receive payments in your woocommerce shop you must install a payment processor like Btcpayserver.

Find all the code in the github repo

The operation of this snippet is based on a series of filters that you must add at the end of the functions.php file of your theme.

These snippets make a call to the oracle to convert the price of our products, this oracle is https://blockchain.info/tobtc and you can find it in the code like this https://blockchain.info/tobtc?currency=$woo_currency&value=$total

Basically we pass the currency set in our shop ($woo_currency) and then the total price ($total), this way we receive the response with the price equivalence in sats and then we show in the front end adding filters, for example add_filter( 'woocommerce_get_price_html', 'woo_product_price_2sats');

If you want to configure to display prices in btc rather than satoshis just change the value of the $data variable within each filter from $data = number_format(ltrim(curl_exec($ch), "0.")); to $data = curl_exec($ch);

(Bonus) I have configured my snippet to show an svg image with the satoshi symbol before the price in this way: "<img class='sat-favicon' src='/wordpress/wp-content/uploads/2022/10/Satoshi-regular-ellipse.svg' width=40px;/><span class='btc-price'>{$data} Sats&nbsp;</span>";

Feel free to play around with this snippet and share your experience! Encouragement to all who work and share to facilitate adoption!🔥🔥 From a pleb to the plebs!

related

Might use this in a SatSale PR! We have a woocommerce plugin and this addition would be great

No way man! it would be awesome if this little piece of code contributed something to the Satsale project!!! I love the project and i was running a test instance on my node. Do you do the PR or do I do it?

A little credit for writing the original code? 🤷🏻‍♂️ https://stacker.news/items/79553

Ok, I hope I didn't bother you. All credits to the code in the snippet that makes the request to blockchain info and returns the btc amount for @gd

Not at all, just good form to give reference

thank you! ill be checking this one out!

@gzoo encountered a couple issues:

  1. the fiat-price is dropping trailing zeros. for example, 100 will appear as 1

  2. enabling the two bottom filters 'woocommerce_cart_item_price' and 'woocommerce_cart_item_subtotal' break the cart. appears to relate to the $total = floatval line

i'm running: wordpress 6.0.2 woocommerce 7.0.0 btcpay for woocommerce v2 1.1.1 storefront theme by woocommerce

Hi tanaris! thanks for your feedback :)

  1. this is because in my woocommerce I have configured two decimals in the price and in the function woo_product_price_2sats I format the price in $product_p_formated with rtrim($total, ".0"); to avoid having zeros that I don't need. You can solve it in a very simple way, either adding two decimals in the woocommerce price settings (woocommerce>settings) or commenting that line of code and in the fiat price print print "<span class='fiat-price'>&nbsp;{$product_p_formated} €</span>"; replace $product_p_formated with $total.
1function woo_product_price_2sats() { 2 global $woo_currency; 3 $all_meta_data = get_post_meta(get_the_ID()); 4 $total=$all_meta_data['_regular_price'][0]; 5 $price_oracle_url = "https://blockchain.info/tobtc?currency=$woo_currency&value=$total"; 6 $ch = curl_init($price_oracle_url); 7 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 8 curl_setopt($ch, CURLOPT_HEADER, 0); 9 $data = number_format(ltrim(curl_exec($ch), "0.")); 10 $product_p_formated=rtrim($total, ".0"); 11 curl_close($ch); 12 print "<span class='btc-price'>{$data} Sats&nbsp;</span>"; 13 print "<span class='fiat-price'>&nbsp;{$product_p_formated} €</span>"; 14}
  1. Yes, I've had those problems too (that's why they appear disabled), it's because woocommerce doesn't have a filter to differentiate between the cart page and the minicart and that breaks it, however I already have a way to do it by digging in the woocommerce template files, I haven't had time to develop it yet, but it's the starting point.

good deal gzoo. appreciate you taking the time to respond and layout a solution here. ill look forward to your revision with cart/minicart fixes. thanks again!