Skip to content

Instantly share code, notes, and snippets.

@ndeet
Last active September 12, 2022 20:34
Show Gist options
  • Save ndeet/b0dfe5be494d5565a281b8009e0685aa to your computer and use it in GitHub Desktop.
Save ndeet/b0dfe5be494d5565a281b8009e0685aa to your computer and use it in GitHub Desktop.
How to use Sats only on WooCommerce with BTCPay Server

This is a way to Sats prices on your WooCommerce (WC) store products by using BTC (Bitcoin) currency that gets shipped with WC. It does still show BTC on checkout pages and on the backend.

So internally WC will use BTC and you need to enter amounts in BTC, e.g. 0.00002100 but on the frontend for product pages it will show 2100 Sats

1. You need to switch your store to use BTC / Bitcoin

Navigate to WC settings: WooCommerce -> Settings -> General (tab): "Currency options" Currency: select Bitcoin (B) Currency position: select Right with space Thousand separator: enter what you prefer ,, or . or sth else Decimal separator: enter what you prefer . or , Number of decimals: enter 8

2. You need to place the following code snippet

You can do that either in your themes functions.php or somewhere else where you have your customization code.

// Override price output on frontend from BTC to Sats.
add_filter( 'woocommerce_get_price_html', 'wcsatsmod_alter_price_display', 9999, 2 );

function wcsatsmod_alter_price_display( $price_html, $product ) {

	// Abort on backend.
	if ( is_admin() ) {
		return $price_html;
	}

	// Abort if not using BTC.
	if ( get_woocommerce_currency() !== 'BTC' ) {
		return $price_html;
	}

	return wcsatsmod_format_btc_as_sats( $product );
}

// Helper to format BTC to Sats
function wcsatsmod_format_btc_as_sats( $product ) {
	$btcPrice = wc_get_price_to_display( $product );

	// Convert BTC to Sats.
	$satoshis = $btcPrice * 100000000;

	$price_format = [
		'ex_tax_label'       => false,
		'currency'           => 'BTC',
		'decimal_separator'  => wc_get_price_decimal_separator(),
		'thousand_separator' => wc_get_price_thousand_separator(),
		'decimals'           => 0,
		'price_format'       => get_woocommerce_price_format(),
	];

	return wc_price( $satoshis, $price_format );
}

// Override BTC symbol on product pages with 'Sats'.
add_filter( 'woocommerce_currency_symbol', 'wcsatsmod_sats_symbol', 10, 2 );

function wcsatsmod_sats_symbol( $currency_symbol, $currency ) {
	// Keep backend and checkout pages as is.
	if ( is_admin() || is_cart() || is_checkout() ) {
		return $currency_symbol;
	}

	if ( $currency === 'BTC' ) {
		$currency_symbol = 'Sats';
	}

	return $currency_symbol;
}

Done. Keep in mind that in the backend you will still see and use BTC. So if you have a product that costs 21000 Sats you need to enter 0.00021000

@ndeet
Copy link
Author

ndeet commented Sep 12, 2022

This is not needed anymore BTCPay Server for Woocommerce V2 plugin as of version v1.1.0 has SAT (Satoshis/Sats) as currency built in. See "Sats-Mode" in the settings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment