Live Coding a Simple WooCommerce Checkout Currency Switcher

I definitely need a EUR/USD switcher in my Woo shop, and I’d love to try implementing it without a plugin. Let’s code it live!

Hosted by Rodolfo Melogli

Masterclass overview

My WooCommerce website sells plugins, courses, memberships, sponsorships in USD only – and that’s because WordPress developers are used to buy in that currency.

However, my PayPal and Stripe fees for currency conversion (so, when a EUR customer using a EUR card buys a USD product) are somewhat ridiculous and I’d love to decrease them.

Therefore I really, really, want to try implementing a simple currency switcher on my WooCommerce checkout page, offering to EU customers an optional switch to EUR (which means I’ll also need to convert prices to EUR based on a custom USD/EUR conversion rate).

In this class, we will first figure out how to add a custom checkout field (a checkbox in this case) and how to show it conditionally (only when the billing country uses EUR in this case). After that, if the checkbox is checked, we will create a function that will update the order currency accordingly, as well as changing the prices.

If there will be enough time, we will also try to get the correct daily exchange rate from an external API.

This is an amazing opportunity to see how a simple yet effective functionality can be coded, so that you can learn a new thing or two for your WooCommerce website or your WooCommerce clients. It’s also a great opportunity to hang out with like-minded professionals during the live class.

Video Recording & Materials

Code

add_filter( 'woocommerce_checkout_fields' , function( $fields ) {
	$fields['billing']['billing_currency'] = array(
		'label' => 'Select currency',
		'type' => 'select',
		'options' => array( 'EUR' => 'Euro', 'USD' => 'Dollars' ),
		'class' => array( 'form-row-wide update_totals_on_change' ),
		'clear' => true,
		'priority' => 41,
		'required' => true,
	);
	return $fields;
});

add_action( 'woocommerce_checkout_update_order_review', function( $post_data ) {
	parse_str( $post_data, $output );
	if ( $output['billing_currency'] ) {
		WC()->session->set( 'billing_currency', sanitize_text_field( $output['billing_currency'] ) );
	}
});

add_filter( 'woocommerce_checkout_get_value', function( $value, $field_name ) {
	if ( $field_name == 'billing_currency' ) {
		$value = WC()->session->get( 'billing_currency' ) ? WC()->session->get( 'billing_currency' ) : $value;
	}
	return $value;
}, 9999, 2 );

add_filter( 'woocommerce_currency', function( $currency ) {
	if ( is_admin() ) return $currency;
	if ( WC()->session->get( 'billing_currency' ) ) {
		$currency = WC()->session->get( 'billing_currency' );
	}
	return $currency;
});

add_action( 'woocommerce_before_calculate_totals', function( $cart ) {
	
	if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; 
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
	
	if ( WC()->session->get( 'billing_currency' ) && WC()->session->get( 'billing_currency' ) !== get_option( 'woocommerce_currency' ) ) {
	
		// we have dollars!
		// 1 EUR to 1.07 USD
		
		foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
			
			$product = $cart_item['data'];
			$price = $product->get_price();
			$product->set_price( $price * 1.07 );
			
		}
		
	}

});

add_filter( 'woocommerce_package_rates', function( $rates, $package ) {

	if ( WC()->session->get( 'billing_currency' ) && WC()->session->get( 'billing_currency' ) !== get_option( 'woocommerce_currency' ) ) {
		
		foreach ( $rates as $rate_id => $rate ) {
			$rate->cost = $rate->cost * 1.07;
		}
		
	}

	return $rates;
}, 9999, 2 );

add_action( 'woocommerce_after_checkout_form',function() {
   wc_enqueue_js( "
	  if ( $('#billing_country') == 'US') {
         $('#billing_currency_field').show();
      } else {
         $('#billing_currency_field').hide();
      }
      $('#billing_country').on('change',function() {
         if ($(this).val() == 'US') {
            $('#billing_currency_field').show();
         } else {
            $('#billing_currency_field').hide();
         }
      });
   " );
});

What you’ll learn

How to add a custom checkout field, conditionally
How to set the currency on the Woo checkout page
How to update prices and reload the checkout with Ajax

Requirements

Basic knowledge of PHP and WordPress hooks

Upcoming masterclasses

Available recordings

1 2
Leave a Reply

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