• Resolved dkydev

    (@dkydev)


    I’ve implemented the code I found here: https://currency-switcher.com/possible-delivery-country-change-currency/
    And I also changed my GeoIP Rules [US = US, Canada = Canada]

    However, if you change your address during checkout it doesn’t change the currency you pay in. Please help.

    The Code in Child Theme

    
    add_action('woocommerce_checkout_update_order_review', function($data) {
        global $WOOCS;
     
        if (is_string($data)) {
           parse_str($data, $data);
        }
        //***
        $_currency = $WOOCS->get_currency_by_country($data['shipping_country']);
        if (!empty($_currency)) {
           $WOOCS->set_currency($_currency);
        }
    }, 9999);
    

    The page I need help with: [log in to see the link]

Viewing 13 replies - 1 through 13 (of 13 total)
  • Plugin Author RealMag777

    (@realmag777)

    Hello

    This code only works for checkout page, not for cart. Make please your attention here: https://c2n.me/44q55xV.pnghttps://dev.maxmind.com/geoip/legacy/codes/iso3166/ – change “Canada” to “CA” – I think because of it $data[‘shipping_country’] gives wrong country, and by default USD is there

    Thread Starter dkydev

    (@dkydev)

    Hi RealMag777!

    Thanks for the reply, ya I’ll give that a try I’ve been tweaking the code but to no avail. it keeps reverting to what shipping country you input first (ie. <GeoIP country set> or <if I allow you to choose a country from cart page>) the checkout currency will not change dependent of the shipping country selected.

    https://i.imgur.com/6VOj0Sn.png

    Thread Starter dkydev

    (@dkydev)

    Hey Support Forum!

    I may have found a solution but it seems to have a few caveats:

    • It doesn’t update by country it only changes with ZIP or State
    • Seems to not work with Google autofill, must be typed in, or adjusted after autofill (I think clicking in the text box works)
    • And I don’t really know what I’m doing just a trial and error ting

    So, I was wondering if you knew if there was a way to clean up the code? and or make it function better. Do you know if anything I wrote would have problems with the plugin and or Woocommerce?

    
    
    #--------------------------------------------------------------------------------
    # 	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
        global $WOOCS, $woocommerce;
    
        // Checking when you're at the checkout page
        if(is_checkout()){
            global $WOOCS, $woocommerce;
    
            // Hopefully will change when customer changes shipping country
            if($woocommerce->customer->get_shipping_country() == 'CA'){
            	// Force currency change from #2
            	$WOOCS->set_currency('CAD');
        	}
        	else{
        		$WOOCS->set_currency('USD');
        	}
    	}
    });
    • This reply was modified 2 years, 8 months ago by dkydev.
    Plugin Author RealMag777

    (@realmag777)

    Hello

    This code should not cause problems with this plugin and woocommerce. You can delete this line – https://c2n.me/44sg7B0.png and change this line – https://c2n.me/44sggry.png :

    if (class_exists('WOOCS') AND is_checkout()) {

    Also autofill doesn’t seem to create change, must be typed in? – it depends on this hook – https://c2n.me/44sgWOl.png – Unfortunately this plugin cannot change this.

    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: http://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);

    Plugin Author RealMag777

    (@realmag777)

    Thank you for cooperation!

    Thread Starter dkydev

    (@dkydev)

    Thanks

    Plugin Author RealMag777

    (@realmag777)

    Welcome!

    I have multi-currencies in my shop usd eur aud cad and gbp using an interactive plugin “include with the theme” which adapt the currencies regarding the IP address, but I want to convert all the currencies to gbp at checkout via PayPal, to avoid expensive exchanges rates at PayPal
    Is it possible to do that with your plugin
    Thanks

    Plugin Author RealMag777

    (@realmag777)

    Thread Starter dkydev

    (@dkydev)

    @neilmahaseth

    Hey I was wondering if you could help me out.

    You mentioned that:

    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.

    And I still can’t figure out how to fix the currency not switching if using Google Autofill. How do you create custom hook ajax

    Hello Guys Thanx for Code it’s working but one probleam with me

    When i change checkout country order review(Pricing table) not update please help me to solve this probleam

    • This reply was modified 2 years, 2 months ago by akaygaur.
    Plugin Support mediawebster

    (@mediawebster)

    Hello @akaygaur

    Please drop me exact link to the issue

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘How to default currency to shipping address’ is closed to new replies.