I’ve tried many currency plugins for WooCommerce, but none of them worked well. Some of them were not compatible with coupons, WooCommerce Subscriptions or poorly coded, so I decided to write a tiny WordPress plugin to change the price and currency based on the billing country.
What it does?
1. If the billing country is not “DE” (Germany), then it gets the price from a custom field “regular_price_usd”, which is created by ACF plugin.
2. If the billing country is not “DE” (Germany), then it changes the currency to USD.
3. If currency is USD, disables TAX calculation.
Can be easily adopted to use more countries and currencies. You can also implement GeoIP and change prices by IP location or browser language on the fly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
<?php /** * Plugin Name: Custom_Currency * Description: Changes the price and currency based on the customer billing country. * Author: Dominykas Gel * Author URI: http://www.dominykasgel.com * Version: 1.0.0 */ if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly } class Custom_Currency { public function __construct() { if ( ! is_admin() ) { add_filter( 'woocommerce_product_get_price', array( $this, 'filter_price' ), 10, 2 ); add_filter( 'woocommerce_currency', array( $this, 'existing_currency_symbol' ) ); add_filter( 'wc_tax_enabled', array( $this, 'woocommerce_tax_enabled' ) ); } } /* * Filters price based on the billing country. * */ public function filter_price( $price, $product ) { $price_in_usd = get_field( 'regular_price_usd', $product->get_id() ); // get USD price from a custom field $countries = array( 'DE' ); if ( $price_in_usd && ! in_array( WC()->customer->get_billing_country(), $countries ) ) { return absint( $price_in_usd ); } else { return $price; } } /* * Filters currency based on the billing country. * */ public function existing_currency_symbol( $currency ) { $countries = array( 'DE' ); if ( ! in_array( WC()->customer->get_billing_country(), $countries ) ) { return 'USD'; } return $currency; } /* * Disables tax for a certain currency. * */ public function woocommerce_tax_enabled( $status ) { if ( get_woocommerce_currency() == 'USD' ) { return false; } return $status; } } $custom_currency = new Custom_Currency(); |