Close-Up Photo of Wooden Jigsaw Map
| |

WooCommerce Function of the Week: get_european_union_countries

You can use get_european_union_countries() for currency switching, to display conditional notices, content, or fields. Basically, whenever there is a country dropdown, get_european_union_countries() may be what you need.

Before we dive into WooCommerce again, here's a little historical context for the function we're going to look at this week: get_european_union_countries.

In 1957, Belgium, France, Italy, Luxembourg, the Netherlands, and West Germany signed the Treaty of Rome, which created the European Economic Community. The European Union was formally established with the Maastricht Treaty on 1 November 1993. There are now 27 EU members, yet the list is always subject to change. With Brexit, the United Kingdom left the EU. There is talk currently about Ukraine joining.

Thankfully, WooCommerce developers coded a function called get_european_union_countries(), which returns an array of … countries in the EU! (Did you see that coming?) Once again, we can rely on a lazy WooCommerce function and stop worrying about who is and who is not in the EU.

But why would you ever need to use get_european_union_countries()? Let's look at its syntax, its arguments, and some possible uses.

Function Syntax

You can find the european_union_countries() function under \woocommerce\includes\class-wc-countries.php:

/**
 * Gets an array of countries in the EU.
 *
 * @param  string $type Type of countries to retrieve. Blank for EU member countries. eu_vat for EU VAT countries.
 * @return string[]
 */
public function get_european_union_countries( $type = '' ) {
	$countries = array( 'AT', 'BE', 'BG', 'CY', 'CZ', 'DE', 'DK', 'EE', 'ES', 'FI', 'FR', 'GR', 'HR', 'HU', 'IE', 'IT', 'LT', 'LU', 'LV', 'MT', 'NL', 'PL', 'PT', 'RO', 'SE', 'SI', 'SK' );

	if ( 'eu_vat' === $type ) {
		$countries[] = 'MC';
	}

	return apply_filters( 'woocommerce_european_union_countries', $countries, $type );
}

There's nothing very complex here β€” a hard-coded list of EU member country codes, and the function returns this array. Simple!

A couple of important notes:

  • The function accepts a parameter, $type. This can either be left blank or set to "eu_vat", in which case MC would also be included in the returned list of countries for Monaco, which is not a EU member, but it is subject to European VAT/tax.
  • The function provides a filter hook, woocommerce_european_union_countries, which you can use in your customization should you need to remove or add countries to the list.

Let's imagine you need to get a list of European countries β€” you'd notice many are not in the list as they don't belong to the EU. This includes Albania, Andorra, Armenia, Azerbaijan, Belarus, Bosnia and Herzegovina, Georgia, Iceland, Kosovo, Liechtenstein, Moldova, Montenegro, North Macedonia, Norway, Russia, San Marino, Serbia, Switzerland, Turkey, Ukraine, United Kingdom, and Vatican City. With the woocommerce_european_union_countries filter you can add them and return the full list of European countries.

Are you wondering if this is a political history post or a WooCommerce development tutorial? I don't blame you. Let's look at the case studies I promised to show how get_european_union_countries() might be used in a real-world scenario.

Case Studies

Why is get_european_union_countries() useful to WooCommerce devleopers?

Well, first of all it's important to understand why WooCommerce has this function and what it uses it for.

By doing a quick file search through the WooCommerce plugin, we can see get_european_union_countries() is called at least 3 times:

  1. The get_vat_countries() function merges the get_european_union_countries() array with another manual array of countries that use “VAT.” This is used in the WooCommerce back end and front end to call taxes either VAT or TAX, based on the country code.
  2. get_automated_support_countries() merges the list of EU countries with ‘US', ‘CA', and ‘AU' in order to return the list of countries that support automated tax calculations within WooCommerce.
  3. get_note() uses get_european_union_countries() to check if your store's country belongs to the EU. If it is and the WooCommerce AvaTax plugin is active, a banner ad is displayed to ask the store manager to install the EU VAT Number extension.

When I first looked into get_european_union_countries(), I was expecting it to be used more extensively within WooCommerce itself, but that's it. It's still useful for developers of course because we can come up with additional ways to use the function and save on development time.

You can use get_european_union_countries() for currency switching, to display conditional notices, content, or fields. Basically, whenever there is a country dropdown, get_european_union_countries() may be what you need.

Let's analyze two more detailed scenarios for using this function.

Set Currency to EUR if the Order Billing Country is in the EU

I issue a lot of manual orders on Business Bloomer, where my main currency is USD, like most of the WordPress ecosystem.

While that is a great currency to sell memberships, plugins, and online courses, EUR is the currency I actually use myself. If I'm on the receiving end of transactions form countries within the EU where EUR is the main currency, I pay lower fees, have greater trust, and my PayPal/Stripe accounts are filled with euros, so I pay no conversion fees. Naturally I want my customers to at least have the option to pay in EUR!

The way I coded this is fairly simple β€” this is straight out of my own functions.php:

/**
 * @snippet       Set new manual order currency | WooCommerce Admin
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 6
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
 
add_action( 'woocommerce_new_order', 'bbloomer_update_order_currency_on_creation', 9999, 2 );

function bbloomer_update_order_currency_on_creation( $order_id, $order ){
    if ( $order && $order->is_created_via( 'admin' ) ) { 
		if ( get_post_meta( $order_id, '_billing_country', true ) && in_array( get_post_meta( $order_id, '_billing_country', true ), WC()->countries->get_european_union_countries( 'eu_vat' ) ) ) {	
			$order->set_currency( 'EUR' );
			$order->save();
		}
	}
}

What It Does: When I create and save a new order from the WooCommerce backend and the billing country belongs to the EU, the order currency is set to EUR by default.

There is another piece to this. If the order is manually set to the status of pending and the customer still needs to pay online, I also need to make EUR the default currency on the payment screen. Here's how I do that:

/**
 * @snippet       Set currency | WooCommerce Order Pay page
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 6
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
 
add_filter( 'woocommerce_currency', 'bbloomer_switch_currency' );

function bbloomer_switch_currency( $currency ) {
	if ( is_wc_endpoint_url( 'order-pay' ) && in_array( WC()->customer->get_billing_country(), WC()->countries->get_european_union_countries( 'eu_vat' ) ) ) {
		return 'EUR';
	}
	return $currency;
}

In this way I've created a multi-currency setup without any plugins! Cool, ha?!

Show Conditional Content if the Order Billing Country is in the EU

I use get_european_union_countries() in my PDF invoices as well.

  1. If the billing country is Italy, I need to show certain messages in Italian by law.
  2. On the other hand, if the billing country belongs to the EU array, I need to display a notice called “Reverse Charge.” (This is for intra-EU transactions when the VAT is recorded by the buyer instead of the seller.)
  3. Otherwise, the function will show a message for extra-EU transactions.

Here's my code:

/**
 * @snippet       Display VAT notices | WooCommerce PDF Invoices & Packing slips
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 6
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_action( 'wpo_wcpdf_after_order_details', 'bbloomer_invoice_dpr_info', 9999, 2 );

function bbloomer_invoice_dpr_info( $template_type, $order ) {
	if ( $order->get_billing_country() == "IT" ) {
		?>
		<?php if ( $order->get_total() > 77.47 ) echo '<p>(*) Imposta di bollo assolta ai sensi dell\'art. 13 della Tariffa allegata al DPR 26.10.72, n. 642</p>'; ?>
		<p>Operazione fuori campo IVA ai sensi dell'art. 1, commi 54-89, L. 23/12/2014, n. 190</p>
		<p>Prestazione non soggetta a ritenuta d'acconto ai sensi del comma 5.2 del Provvedimento Agenzia delle entrate del 22.12.2011 n. 185820</p>		
		<?php
	} elseif ( in_array( $order->get_billing_country(), WC()->countries->get_european_union_countries( 'eu_vat' ) ) && ! empty( get_post_meta( $order->get_id(), '_billing_vat_number_eu', true ) ) ) {
		// Intra EU Service
		?>
		<p>Invoice subject to Reverse Charge rule in the country of receipt according to Italian law art. 7-TER, D.P.R. n. 633/1972</p>	
		<?php
	} else {
		// Extra EU Service
		// print_r($order);
		?>
		<?php /* if ( $order->get_total() > 77.47 ) echo '<p>(*) Cost of €2 Italian stamp duty included in payment according to the Tariffa attachment, art. 13, DPR 26.10.72, n. 642</p>'; */ ?>
		<p>Invoice exempt from VAT/TAX according to Italian law art. 7, D.P.R. n. 633/1972</p>		
		<?php
	}
}

Wherever you have access to $order->get_billing_country(), you may use get_european_union_countries() for conditional content. I've seen an example for using it in PDF invoices. It could also apply to the “Thank You” screen, customer account pages, order admin pages, and more.

We hope this guide has been educational and useful for you! Tell us in the comments how you're using get_european_union_countries() and dealing with multi-currency situations in WooCommerce in your own code. πŸ’Ά

Similar Posts

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.