Advertisement
businessdad

WooCommerce Currency Switcher - Set currency for legacy API

Sep 21st, 2017
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.75 KB | None | 0 0
  1. /**
  2.  * Aelia Currency Switcher - Force currency for legacy API calls.
  3.  *
  4.  * HOW TO USE THIS CODE
  5.  * Simply add the code to the bottom of your theme's functions.php file, and it
  6.  * will run automatically. For more information: https://www.skyverge.com/blog/add-custom-code-to-wordpress/
  7.  *
  8.  * GPL DISCLAIMER
  9.  * Because this code program is free of charge, there is no warranty for it, to the extent permitted by applicable law.
  10.  * Except when otherwise stated in writing the copyright holders and/or other parties provide the program "as is"
  11.  * without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of
  12.  * merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the program
  13.  * is with you. should the program prove defective, you assume the cost of all necessary servicing, repair or correction.
  14.  *
  15.  * Need a consultation, or assistance to customise this code? Find us on Codeable: https://aelia.co/hire_us
  16.  */
  17.  
  18. /**
  19.  * Forces the selected currency to GBP for legacy API requests, unless another
  20.  * currency was passed via the URL.
  21.  *
  22.  * @param string selected_currency
  23.  * @return string
  24.  */
  25. function aelia_set_currency_for_api_calls($selected_currency) {
  26.   // Force selected currency to GBP for API calls, unless a
  27.   // currency was passed via the URL
  28.   if(empty($_GET['aelia_cs_currency'])) {
  29.     $selected_currency = 'GBP';
  30.   }
  31.   return $selected_currency;
  32. }
  33.  
  34. /**
  35.  * Intercepts legacy API requests to force the currency to GBP, bypassing geolocation.
  36.  *
  37.  * @param string api_request
  38.  */
  39. add_action('woocommerce_api_request', function($api_request) {
  40.   add_filter('wc_aelia_cs_selected_currency', 'aelia_set_currency_for_api_calls', 30);
  41. }, 10, 1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement