You are here

function commerce_multicurrency_set_user_currency_code in Commerce Multicurrency 7

Store the currency_code to use for the user.

Invokes the rules event commerce_multicurrency_user_currency_set.

Parameters

string $currency_code: The currency code to set the user currency to.

boolean $overwrite_cookie: Set to FALSE if the currency shouldn't be changed if the cookie already exists.

4 calls to commerce_multicurrency_set_user_currency_code()
CommerceMulticurrencyTestCase::testNotOverwrittingCurrency in tests/commerce_multicurrency.test
Test that the currency switching works as expected.
CommerceMulticurrencyTestCase::testSetCurrency in tests/commerce_multicurrency.test
Test that the currency switching works as expected.
commerce_multicurrency_selector_form_submit in ./commerce_multicurrency.module
Submit handler for the currency selector block form.
commerce_multicurrency_set_user_currency_code_callback in ./commerce_multicurrency.module
Handles requests to the currency set menu callback.

File

./commerce_multicurrency.module, line 429
Enhancements for the commerce currency support.

Code

function commerce_multicurrency_set_user_currency_code($currency_code, $overwrite_cookie = TRUE) {
  if ($overwrite_cookie || empty($_COOKIE['Drupal_visitor_commerce_currency'])) {
    $enabled_currencies = commerce_currencies(TRUE);
    if (isset($enabled_currencies[$currency_code])) {
      $old_currency_code = commerce_multicurrency_get_user_currency_code();

      // Inject currency into the static cache.
      $current_currency_code =& drupal_static('commerce_multicurrency_get_user_currency_code', FALSE);
      $current_currency_code = $currency_code;

      // Set cookie.
      user_cookie_save(array(
        'commerce_currency' => $currency_code,
      ));
      rules_invoke_event('commerce_multicurrency_user_currency_set', $currency_code, $old_currency_code);

      // Refresh the order. Triggers also the update of line item prices.
      // @see commerce_multicurrency_commerce_cart_line_item_refresh().
      if (module_exists('commerce_cart')) {
        global $user;
        if ($order = commerce_cart_order_load($user->uid)) {
          commerce_cart_order_refresh($order);
        }
      }
    }
  }
}