2

i'm looking for a solution that allows me to change the cost of every shipping zones because i need to have a rounded value also in my second currency (from € to yen for Japan). I used WC_Geolocation::geolocate_ip() to change the currency dynamically based on the IP adress, but i can't find a solution to change the shipping zones cost. An example to explain:

    $location = WC_Geolocation::geolocate_ip();
    $country = $location['country'];
    if($country === "JP"){
    //do some code to change every shipping zone cost with a custom value
    //(different for every shipping zone)
    } 

I hope I was clear in my explanation.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Alessio
  • 21
  • 3

1 Answers1

2

Updated

You may have to "Enable debug mode" in general shipping settings under "Shipping options" tab, to disable temporarily shipping caches.

The following code will change the shipping method cost on custom calculations for a specific country (here Japan):

add_filter( 'woocommerce_package_rates', 'custom_shipping_rates_based_on_country', 20, 2 );
function custom_shipping_rates_based_on_country( $rates, $package ) {

    // ONLY for Japan
    if( $package['destination']['country'] !== 'JP' )
        return $rates;

    // Loop through shipping methods rates
    foreach( $rates as $rate_key => $rate ){

        // Excluding free shipping method
        if( $rate->method_id !== 'free_shipping') {

            // Get initial shipping rate cost
            $initial_cost = $rate->cost;

            ## ---- START ------------------------------------------- ##

            // Add your calculations and settings

            // Rounding decimal setting
            $rounding_decimals = 2;

            // Conversion rate
            $conversion_rate = 1.25;

            // New calculated cost
            $new_cost = $initial_cost * $conversion_rate;

            ## ----  END  ------------------------------------------- ##

            // Set the rate cost (rounded with 2 decimals)
            $rates[$rate_key]->cost = round( $new_cost, $rounding_decimals );

            // Taxes rate cost (if enabled)
            foreach ($rate->taxes as $key => $tax){
                if( $rate->taxes[$key] > 0 ){

                    // Calculating the tax rate unit
                    $tax_rate = $rate->taxes[$key] / $initial_cost;

                    // Calculating the new tax cost
                    $new_tax_cost = $tax_rate * $new_cost;

                    // set the new tax cost
                    $taxes[$key] = round( $new_tax_cost, $rounding_decimals );

                    $has_taxes = true;
                } else {
                    $has_taxes = false;
                }
            }
            if( $has_taxes )
                $rates[$rate_key]->taxes = $taxes;

        }
    }

    return $rates;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Don't forget to enable back shipping cache.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thankyou veru much, but can i use this to change my shipping zones price instead of my shipping method? There are a lot of countries where i send products and i can't repeat this code for everyone. Can i retrieve shipping zones setting by name and change the cost value? – Alessio Oct 11 '18 at 17:27
  • @Alessio There is no prices (or costs) in shipping zones themselves, but in the shipping methods that are enabled for a shipping zone… This hook is the correct hook to make changes on shipping zones costs. It will only target "Japan" location as you can see in the answer code. – LoicTheAztec Oct 11 '18 at 19:07