Advertisement
businessdad

Aelia Currency Switcher - Force currency at checkout

Mar 18th, 2017
471
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.35 KB | None | 0 0
  1. /**
  2.  * Extract from support article https://aelia.freshdesk.com/solution/articles/3000010676
  3.  *
  4.  * Example 6 - Forcing checkout in a specific currency
  5.  * If you wish to allow customers to browse the site in a currency of their choice,
  6.  * but force them to complete the checkout in a specific currency, the following code
  7.  * shows an example of how you can do that.
  8.  *
  9.  * Need help customising the code for your need? Hire us on Codeable: http://bit.ly/codeable_aelia
  10.  */
  11.  
  12. /**
  13.  * Adds parameters to the Ajax request used to refresh the minicart. The additional
  14.  * parameters will be used to determine the context in which the request was made,
  15.  * so that we can know if we should force the currency because we are on the
  16.  * checkout page.
  17.  *
  18.  * @param array params An array of parameters.
  19.  * @return array
  20.  */
  21. add_filter('wc_cart_fragments_params', function($params) {
  22.   if(is_checkout()) {
  23.     if(strpos($params['wc_ajax_url'], '?') > 0) {
  24.       $params['wc_ajax_url'] .= '&';
  25.     }
  26.     else {
  27.       $params['wc_ajax_url'] .= '?';
  28.     }
  29.     // Add a parameter to the Ajax request, to keep track that it was triggered
  30.     // on the checkout page
  31.     $params['wc_ajax_url'] .= 'aelia_context=checkout';
  32.   }
  33.   return $params;
  34. });
  35.  
  36. /**
  37.  * Determines if we are processing an Ajax request triggered on the checkout page.
  38.  *
  39.  * @return bool
  40.  */
  41. function aelia_is_checkout_ajax_request() {
  42.   if(defined('DOING_AJAX')) {
  43.     // The "update_order_review" request updates the totals on the checkout
  44.     // page
  45.     if($_REQUEST['wc-ajax'] === 'update_order_review') {
  46.       return true;
  47.     }
  48.  
  49.     // The "get_refreshed_fragments" updates the minicart. We only need to take
  50.     // it into account if we are on the checkout page
  51.     if(($_REQUEST['wc-ajax'] === 'get_refreshed_fragments') &&
  52.        ($_REQUEST['aelia_context'] === 'checkout')) {
  53.       return true;
  54.     }
  55.   }
  56.   return false;
  57. }
  58.  
  59. /**
  60.  * Forces the checkout currency to USD.
  61.  *
  62.  * @param string selected_currency The currency selected originally.
  63.  * @return string
  64.  */
  65. add_filter('wc_aelia_cs_selected_currency', function($selected_currency) {
  66.   // If we are on the checkout page, or handling an Ajax request from that page,
  67.   // force the currency to USD
  68.   if(is_checkout() || is_checkout_ajax_request()) {
  69.     $selected_currency = 'USD';
  70.   }
  71.   return $selected_currency;
  72. }, 50);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement