Created
December 1, 2018 08:45
-
-
Save varun-pluginhive/314791bd8f864075e43b857feb1e0c72 to your computer and use it in GitHub Desktop.
Snippet to support CodeCanyon WooCommerce Currency Switcher. Note - In Plugin Settings Currency should be set to the fedex account country. No need of Conversion rate. WooCommerce FedEx Shipping Plugin with Print Label - https://www.pluginhive.com/product/woocommerce-fedex-shipping-plugin-with-print-label/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Snippet to support CodeCanyon WooCommerce Currency Switcher. Note - In Plugin Settings Currency should be set to the fedex account country. No need of Conversion rate. | |
| * Created at : 01 Dec 2018 | |
| * Updated at : 01 Dec 2018 | |
| * PluginHive Plugins : https://www.pluginhive.com/plugins/ | |
| * Gist Link : https://gist.github.com/varun-pluginhive/314791bd8f864075e43b857feb1e0c72 | |
| */ | |
| // Rate Request | |
| add_filter( 'xa_fedex_rate_request', function( $request ){ | |
| $fedex_base_currency = 'MXN'; // Currency supported by Woocommerce | |
| $fedex_currency = "NMP"; // Currency Supported by Fedex | |
| if( class_exists('WOOCS') ) { | |
| $woocs = new WOOCS(); | |
| $currencies = $woocs->get_currencies(); | |
| $current_currency = get_woocommerce_currency(); | |
| $fedex_settings = get_option( 'woocommerce_'.WF_Fedex_ID.'_settings', array() ); | |
| $currency_in_request = $request['RequestedShipment']['PreferredCurrency']; | |
| if( ! empty($currencies) && ! empty($current_currency) && ! empty($currencies[$current_currency]['rate']) ) { | |
| $exchange_rate_to_default = (double) $currencies[$current_currency]['rate']; | |
| $exchange_rate_to_fedex = ! empty($currencies[$fedex_base_currency]['rate']) ? (double) $currencies[$fedex_base_currency]['rate'] : 1; | |
| $request['RequestedShipment']['PreferredCurrency'] = $fedex_currency; | |
| // Replace Insured amount | |
| foreach( $request['RequestedShipment']['RequestedPackageLineItems'] as &$line_items ) { | |
| if( ! empty($line_items['InsuredValue']) ) { | |
| // $line_items['InsuredValue']['Currency'] = $fedex_currency; | |
| $line_items['InsuredValue']['Amount'] = ( ( (double) $line_items['InsuredValue']['Amount']) / $exchange_rate_to_default ) * $exchange_rate_to_fedex; | |
| } | |
| } | |
| // Replace Total Insured Value | |
| if( ! empty($request['RequestedShipment']['TotalInsuredValue']) ) { | |
| // $request['RequestedShipment']['TotalInsuredValue']['Currency'] = $fedex_currency; | |
| $request['RequestedShipment']['TotalInsuredValue']['Amount'] = ( ( (double) $request['RequestedShipment']['TotalInsuredValue']['Amount']) / $exchange_rate_to_default ) * $exchange_rate_to_fedex; | |
| } | |
| // Replace Commoditeies Unit price and Custom Value | |
| if( ! empty($request['RequestedShipment']['CustomsClearanceDetail']['Commodities']) ) { | |
| foreach( $request['RequestedShipment']['CustomsClearanceDetail']['Commodities'] as &$commodities ) { | |
| // Convert UnitPrice | |
| if( ! empty($commodities['UnitPrice']['Amount']) ) { | |
| $commodities['UnitPrice']['Amount'] = ( ( (double) $commodities['UnitPrice']['Amount']) / $exchange_rate_to_default ) * $exchange_rate_to_fedex; | |
| } | |
| // Convert Customs Value | |
| if( ! empty($commodities['CustomsValue']['Amount']) ) { | |
| $commodities['CustomsValue']['Amount'] = ( ( (double) $commodities['CustomsValue']['Amount']) / $exchange_rate_to_default ) * $exchange_rate_to_fedex; | |
| } | |
| } | |
| } | |
| //Convert Total Custom Value | |
| if( ! empty($request['RequestedShipment']['CustomsClearanceDetail']['CustomsValue']) ) { | |
| $request['RequestedShipment']['CustomsClearanceDetail']['CustomsValue']['Amount'] = ( ( (double) $request['RequestedShipment']['CustomsClearanceDetail']['CustomsValue']['Amount']) / $exchange_rate_to_default ) * $exchange_rate_to_fedex; | |
| } | |
| } | |
| } | |
| return $request; | |
| }); | |
| // Label Request | |
| if( ! function_exists('ph_fedex_shipment_rate_conversion_request') ) { | |
| function ph_fedex_shipment_rate_conversion_request( $request, $order ) { | |
| $fedex_base_currency = 'MXN'; // Currency supported by Woocommerce | |
| $fedex_currency = "NMP"; // Currency Supported by Fedex | |
| $order_base_currency = $order->get_meta('_woocs_order_base_currency'); | |
| $order_conversion_rate = (double) $order->get_meta('_woocs_order_rate'); | |
| if( class_exists('WOOCS') ) { | |
| $woocs = new WOOCS(); | |
| $currencies = $woocs->get_currencies(); | |
| $exchange_rate_to_fedex = ! empty($currencies[$fedex_base_currency]['rate']) ? (double) $currencies[$fedex_base_currency]['rate'] : 1; | |
| if( ! empty($currencies[$fedex_base_currency]['rate']) ) { | |
| // Cost Conversion For Insurance Value | |
| foreach($request['RequestedShipment']['RequestedPackageLineItems'] as &$line_items ) { | |
| if( ! empty($line_items['InsuredValue']) ) { | |
| $line_items['InsuredValue']['Amount'] = (double) $line_items['InsuredValue']['Amount'] * $exchange_rate_to_fedex; | |
| } | |
| } | |
| // Amount COnversion for TotalInsured Value | |
| if( ! empty($request['RequestedShipment']['TotalInsuredValue']) ) { | |
| $request['RequestedShipment']['TotalInsuredValue']['Amount'] = (double) $request['RequestedShipment']['TotalInsuredValue']['Amount'] * $exchange_rate_to_fedex; | |
| } | |
| // Cost Conversion for Custom Vale | |
| if( ! empty($request['RequestedShipment']['CustomsClearanceDetail']) ) { | |
| $request['RequestedShipment']['CustomsClearanceDetail']['CustomsValue']['Amount'] = ( (double) $request['RequestedShipment']['CustomsClearanceDetail']['CustomsValue']['Amount'] / $order_conversion_rate) * $exchange_rate_to_fedex; | |
| } | |
| // Cost Conversion for Commodities | |
| foreach( $request['RequestedShipment']['CustomsClearanceDetail']['Commodities'] as &$commodities ) { | |
| $commodities['UnitPrice']['Amount'] = (double) $commodities['UnitPrice']['Amount'] * $exchange_rate_to_fedex; | |
| $commodities['CustomsValue']['Amount'] = (double) $commodities['CustomsValue']['Amount'] * $exchange_rate_to_fedex; | |
| } | |
| } | |
| } | |
| return $request; | |
| } | |
| add_filter( 'wf_fedex_request', 'ph_fedex_shipment_rate_conversion_request', 10, 2 ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment