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
Requirements
Upcoming masterclasses
Buying a WooCommerce Store: All You Need to Know
A guide to valuation, negotiation, and acquisition strategies – along with post-acquisition…
Unlocking the Power of WooCommerce Featured Products
Many developers and store owners find themselves unsure about how to effectively…
Available recordings
1-Hour WooCommerce Challenge: Let’s Recreate the Nike Product Page
Join me for a live coding challenge, where I’ll customize the WooCommerce Single Product page…
Live Coding a WooCommerce Mini-Plugin
Join me for a live coding session, while I try to develop a custom, commercial…
WooCommerce AMA with Rodolfo Melogli
Join me for an ‘Ask Me Anything About WooCommerce’ session โ covering customization, development, plugins,…
Maximize Your WooCommerce Potential: Understanding User Behavior with Clarity
Learn how to use the free Microsoft Clarity plugin to record and analyze user behavior…
Mastering WooCommerce Thank You Page Customization: A Plugin-Free Approach
Let’s learn how to personalize the WooCommerce Thank You page with simple code, so that…
Conversion-Focused Redesign Of The WooCommerce Single Product Page
Let’s improve the boring WooCommerce Single Product page and encourage MORE users to convert. Hosted…
Allow Multiple Payments In The Same WooCommerce Order
All deposit / split / partial payment plugins generate an additional order for paying the…
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…
How to Find and Fix Slow Database Queries in WooCommerce
Learn how to resolve slow database queries in WordPress / WooCommerce websites. Use the right…
– BACKED BY –
Is your WooCommerce store prepared for traffic spikes? Improve speeds up to 200% with our
managed WooCommerce hosting. Enjoy scalable server resources, rock-solid security, and 24/7 support.