Limit WooCommerce checkout currencies per product


This code snippet for WooCommerce enables you to use a custom field to limit which currencies are available on the checkout on a per product basis. After setting up the custom field, the currency options on checkout will be limited depending on which product(s) are in the cart.

1) Visit any WooCommerce product you want the price to be displayed as a cryptocurrency. Open the screen options and check to make sure “custom fields” are shown in the editor.

 

2) To limit the currencies available for a specific product during checkout, add a custom field called “mcc_product_currency” to the product. Set the value of the custom field to whatever cryptocurrency symbol MyCryptoCheckout supports such as: BTC, ETH, LTC. For a full list of currencies please visit the supported coins page.

meta-field-products

 

3) Add the code snippet below to your theme’s functions.php file or into a custom plugin.

/**
	@brief		Only allow for some currencies during checkout, depending on the product's mcc_product_currency custom field.
	@details	Use the same mcc_product_currency custom field as specified here to limit the checkout wallet options: https://mycryptocheckout.com/doc/snippets/display-woocommerce-product-prices-in-other-currencies/
	@since		2020-02-16 18:51:41
**/
function mcc_mycryptocheckout_woocommerce_payment_fields_wallet_options( $action )
{
	$items = WC()->cart->get_cart_contents();

	$enabled_currencies = [];
	foreach( $items as $item )
	{
		$product_id = $item[ 'product_id' ];
		$mcc_product_currency = get_post_meta( $product_id, 'mcc_product_currency', true );
		if ( ! $mcc_product_currency )
			return;
		$mcc_product_currency = strtoupper( $mcc_product_currency );
		$mcc_product_currencies = explode( ',', $mcc_product_currency );
		foreach( $mcc_product_currencies as $currency )
			$enabled_currencies[ $currency ][ $product_id ] = $product_id;
	}

	if ( count( $enabled_currencies ) < 1 )
		return;

	$output = [];
	$restrict_to = array_keys( $enabled_currencies );
	foreach( $restrict_to as $currency_id )
		if ( isset( $action->options[ $currency_id ] ) )
			$output[ $currency_id ] = $action->options[ $currency_id ];
	$action->options = $output;
}
add_action( 'mycryptocheckout_woocommerce_payment_fields_wallet_options', 'mcc_mycryptocheckout_woocommerce_payment_fields_wallet_options' );

 

4) Now currency options on checkout will be limited depending on which product(s) are in the cart.

woocommerce-per-product-currency