0
votes

Information

WooCommerce version: 3.6.2

WordPress version: 5.1.1

YITH WooCommerce Request A Quote Premium 2.1.8

Booster Plus 4.3.1 - Prices and Currencies by Country module

Change Quantity on Checkout for WooCommerce Version 1.0


Current situation

1.YITH Manual Quote created in Euro with Shipping country set as Romania.

  1. Customer lands on Checkout from Australia.

    • Gap: Mini cart price shows Australian dollar. See screenshot Mini Cart
  2. Checkout gets refreshed by running below code and shows the correct Order currency - Euro (but not in Mini Cart)! See screenshot Checkout


Objective

Get correct order currency in Mini Cart & Checkout for a Quote in spite of customer's visiting country


Update

After a lot of searching I came up with nothing. Finally I settled for installing Change Quantity on Checkout for WooCommerce plugin for the checkout into one.

I am still stuck though. The currency gets updated AFTER Checkout page is refreshed ONCE.


Code

Using the code from here Update cart shipping data with AJAX in WooCommerce I have managed to refresh the checkout page and the currency shows correctly on load.

add_action( 'wp_footer', 'refresh_shipping_js' );
function refresh_shipping_js() {
    // Only on checkout
    if( is_checkout() && ! is_wc_endpoint_url() ):
    ?>
    <script type="text/javascript">
    jQuery( function($){
        if (typeof wc_checkout_params === 'undefined') 
            return false;

        var refresh = 'yes';

        $.ajax({
            type: "POST",
            url: wc_checkout_params.ajax_url,
            data: ({
                'action': 'updating_shipping',
                'refresh_shipping': refresh,

            }),
            success: function(response) {
                if( response === '1' ) {
                    $(document.body).trigger('update_checkout');
                    console.log('Success: '+response); // For testing (to be removed)
                } else {
                    console.log('Failled: '+response); // For testing (to be removed)
                }
            },
            error:function(error) {
                console.log('Error: '+error); // For testing (to be removed)
            }
        });
    });
    </script>
    <?php
    endif;
}
// function that gets the Ajax data
add_action( 'wp_ajax_updating_shipping', 'updating_shipping' );
add_action( 'wp_ajax_nopriv_updating_shipping', 'updating_shipping' );
function updating_shipping() {
    if ( isset($_POST['refresh_shipping']) && $_POST['refresh_shipping'] === 'yes' ){
        WC()->session->set('refresh_shipping', '1' );
    } else {
        WC()->session->set('refresh_shipping', '0' );
    }
    echo  WC()->session->get('refresh_shipping');
    die(); // Alway at the end (to avoid server error 500)
}

// Function that refresh session shipping methods data
add_action( 'woocommerce_checkout_update_order_review', 'refresh_shipping_methods', 10, 1 );
function refresh_shipping_methods( $post_data ){
    if ( WC()->session->get('refresh_shipping' ) === '1' ) {
        foreach ( WC()->cart->get_shipping_packages() as $package_key => $package ){
            WC()->session->set( 'shipping_for_package_' . $package_key, false );
        }
        WC()->cart->calculate_shipping();
    }
}

Need help

Is there any better way to achieve this?

All help would be highly appreciated

2

2 Answers

1
votes

Theres a few plugins that might help here: https://docs.woocommerce.com/document/handle-multiple-regions-currencies-woocommerce/

One part of it would be to add more than one shipping region. Also I'm not sure if its available there but in the US Jet Pack has an extension that I have used in the past that will automatically calculate tax based on shipping address.

0
votes

In case anyone else has a similar issue, I solved it after installing Price Based on Country for WooCommerce (free) plugin and selecting Load products price in background in it's settings

There was no code required anywhere