businessdad icon

Currency Switcher for WooCommerce - Flat Rate per Country

businessdad | PRO | 02/26/16 01:20:54 PM UTC | 0 ⭐ | 1454 👁️ | Never ⏰ | []
PHP |

2.59 KB

|

None

|

0 👍

/

0 👎

// Add this code to your theme's functions.php to ensure that the free 
// shipping thresholds used by the Flat Rate Shipping per Country plugin
// are converted correctly.
 
/**
 * Fix for the Flat Rate per Country/Region plugin.
 * Processes the shipping method settings, ensuring that the free shipping
 * thresholds are converted to the active currency.
 *
 * @param array shipping_methods The shipping methods to process.
 * @return array The array of shipping methods, with the Flat Rate per Country
 * plugin's settings converted as needed.
 * @author Aelia <[email protected]>
 * @link https://aelia.co
 */
add_filter('woocommerce_shipping_methods', function($shipping_methods) {
    foreach($shipping_methods as $key => $method) {
        if(!is_object($method) && ($method == 'WC_Flat_Rate_Per_Country_Region')) {
            $method = new $method();
        }
        if($method instanceof WC_Flat_Rate_Per_Country_Region) {
            $settings_to_convert = array('world_free_above');
 
            // Build a list of the settings whose value should be converted to the
            // active currency
            $counters = array('region', 'country', 'state');
            foreach($counters as $counter_id) {
                for($idx = 0; $idx < (int)$method->settings['per_' . $counter_id . '_count']; $idx++) {
                    // The shipping plugin uses counters that start from 1
                    $settings_to_convert[] = 'per_' . $counter_id . '_' . ($idx + 1) . '_fr';
                }
            }
 
            foreach($settings_to_convert as $setting_key) {
                if(!empty($method->settings[$setting_key])) {
                    $method->settings[$setting_key] = aelia_convert($method->settings[$setting_key]);
                }
            }
 
            $shipping_methods[$key] = $method;
        }
    }
    return $shipping_methods;
}, 51);
 
 
/**
 * Basic integration with WooCommerce Currency Switcher, developed by Aelia
 * (http://aelia.co). This method can be used by any 3rd party plugin to
 * return prices converted to the active currency.
 *
 * @param double price The source price.
 * @param string to_currency The target currency. If empty, the active currency
 * will be taken.
 * @param string from_currency The source currency. If empty, WooCommerce base
 * currency will be taken.
 * @return double The price converted from source to destination currency.
 * @author Aelia <[email protected]>
 * @link https://aelia.co
 */
function aelia_convert($price, $to_currency = null, $from_currency = null) {
    if(empty($from_currency)) {
        $from_currency = get_option('woocommerce_currency');
    }
    if(empty($to_currency)) {
        $to_currency = get_woocommerce_currency();
    }
    return apply_filters('wc_aelia_cs_convert', $price, $from_currency, $to_currency);
}

Comments