Hi dkydev & RealMag777,
Thank you for sharing the code. I also wanted a similar feature. Using your code this is the custom plugin I have created. This works for me except for the limitations mentioned by dkydev. Which can be overcome by creating a custom hook using ajax.Hope it helps others.
<?php
/**
* Plugin Name: WooCommerce Shipping Country Currency
* Plugin URI: https://www.xyz.com/
* Description: This plugin changes the default currency in case the shipping country changes.
* Version: 1.0
* Author: Neil Mahaseth
* Author URI: https://xyz.com/
**/
#——————————————————————————–
# WOOCS – Default order currency to shipping country
# Attempt #7.1
#——————————————————————————–
/* My attempt at defaulting address to shipping country
* 1. https://currency-switcher.com/possible-delivery-country-change-currency/
* 2. https://currency-switcher.com/force-currency-on-checkout-page/
* 3. https://stackoverflow.com/questions/29378859/woocommerce-get-order-shipping-address-as-separate-fields
* 4. https://stackoverflow.com/questions/31133501/woocommerce-change-currency-depending-on-billing-country
*
* Result: Seems to work. But doesn’t always change with Country seems more connected to ZIP or State?
* Update: Also autofill doesn’t seem to create change, must be typed in?
*/
// Need action from #1 as this should update order at checkout page
add_action(‘woocommerce_checkout_update_order_review’, function($data) {
// I’m pretty sure this is to call libraries. And #4 needs the woocommerce library
// Checking when you’re at the checkout page
if(class_exists(‘WOOCS’) AND is_checkout()){
global $WOOCS, $woocommerce;
// Hopefully will change when customer changes shipping country
if($woocommerce->customer->get_shipping_country() == ‘IN’){
// Force currency change from #2
$WOOCS->set_currency(‘INR’);
}
else{
$WOOCS->set_currency(‘USD’);
}
}
}, 9999);