Page 1 of 2

Forcing Currency by Customer

Posted: Wed Sep 27, 2023 8:27 am
by Portman
I was hoping to get a bit of assistance nutting out a problem I have.

I run a wholesale business that sells products around the world. We have decided to charge customers from our main countries/regions a static price in THEIR OWN currency. I have been able to assign customers different groups based on region using @raiwa 's Wholesale Pro Addon.

Playing around with the file includes/system/versioned/1.0.8.6/currencies.php I have commented out the sections that multipes the price by the currency exchange value.

Now I need to change the currency based on the group that the customer is in. I am assuming that I need to do this in this function;

Code: Select all

    public function set_currency() {
      if (!isset($_SESSION['currency']) || isset($_GET['currency']) || ( (USE_DEFAULT_LANGUAGE_CURRENCY == 'true') && (LANGUAGE_CURRENCY != $_SESSION['currency']) ) ) {
        if (isset($_GET['currency']) && $GLOBALS['currencies']->is_set($_GET['currency'])) {
          $_SESSION['currency'] = $_GET['currency'];
        } else {
          $_SESSION['currency'] = ((USE_DEFAULT_LANGUAGE_CURRENCY == 'true') && $GLOBALS['currencies']->is_set(LANGUAGE_CURRENCY)) ? LANGUAGE_CURRENCY : DEFAULT_CURRENCY;
        }

        $GLOBALS['currency'] =& $_SESSION['currency'];
      }
    }


First, am i correct that this function controlls the currency for the entire site (at this stage I am just intested in changing the layout of the currency/price with correct symbol ($, £, € etc) due to the changes I mentioned above) ?

Second,
I need some help figuring out how to modify it to do what I want;
In simple terms,

I GET the cusomers Group from their record in 'customers' on the database
From there I create a variable who's value will be the same as one of the 'code' values in the currencies table.... EG (USD, AUD, GBP, EUR) I will call this variable 'customer-group'
I can do all that....

What I am stuck with is how to compare the 'customer-group' Value with the 'currencies->code' value and force the currency to reflect the correct currency.

third Where do I save the modified file so I am not changing the core code?

I hope I am not asking too much here, any help will be appreciated even if it's just letting me know if I'm on the right track.

Re: Forcing Currency by Customer

Posted: Wed Sep 27, 2023 9:04 am
by burt
Portman wrote: Wed Sep 27, 2023 8:27 am third Where do I save the modified file so I am not changing the core code?
You can take any file out of /includes/system/versioned/x.x.x.x/
and save in
/includes/system/override/ [create the override folder if necessary]

The other questions are for someone else.

Re: Forcing Currency by Customer

Posted: Wed Sep 27, 2023 10:25 am
by ecartz
That function is run from a database hook. So you don't need to override the class to change that function. You can just change the hook in the database to point at a different function.

Re: Forcing Currency by Customer

Posted: Wed Sep 27, 2023 11:50 pm
by Portman
ecartz wrote: Wed Sep 27, 2023 10:25 am That function is run from a database hook. So you don't need to override the class to change that function. You can just change the hook in the database to point at a different function.
Thanks @ecartz I just looked in the database at the hooks... It looks like there is a whole new area for me to learn...
Is there some info about how these work or could you give me a quick rundown for dummies on how I would go about making this work (as in just an outline - i'm not asking you to do the work for me).

Re: Forcing Currency by Customer

Posted: Sat Sep 30, 2023 8:03 am
by Portman
I have given a go at changing the function set_currency() in includes/system/versioned/1.0.8.6/currencies.php (saved into override folder), and i can almost get it to work... at the moment it stays with the default currency when I log in a new customer, but if I then refresh the page it converts to the correct currency for that customer... I don't think there is anything wrong with the code, but can anyone give me an idea of what may be going wrong?...

Code: Select all

    public function set_currency() {	

	  if (!isset($_SESSION['customer_id']) || ($_SESSION['wholesale_group'] == '0' )) {
		  $_SESSION['currency'] = DEFAULT_CURRENCY;
	  }
		else if($_SESSION['wholesale_group'] > '0' ) {		  
		$_SESSION['currency'] = constant('MODULE_STORE_WHOLESALE_ENTRY_WHOLESALE_' . $_SESSION['wholesale_group']. "_" . strtoupper($_SESSION['language']));			
        } 
       
		$GLOBALS['currency'] =& $_SESSION['currency'];
    }
wholesale_group is the variable that holds a number that identifies which group the customer has been assigned to.

MODULE_STORE_WHOLESALE_ENTRY_WHOLESALE_'wholesale_group'_ENGLISH corresponds to the 3 letter code for currencies as set up in ADMIN->LOCALIZATION->CURRENCIES

Re: Forcing Currency by Customer

Posted: Sun Oct 01, 2023 5:00 pm
by burt
It is likely that the file is loaded before the customers session. On the next page load the session is active, therefore it shows correct at the new page load.

I'm not sure how to combat that.

Re: Forcing Currency by Customer

Posted: Sun Oct 01, 2023 8:17 pm
by ecartz
Call set_currency in the postLogin hook, after the customer ID is set. I.e. call it a second time on the same page load. Although I'm not quite sure why it isn't already refreshing after login.

Re: Forcing Currency by Customer

Posted: Mon Oct 02, 2023 10:04 am
by Portman
ecartz wrote: Sun Oct 01, 2023 8:17 pm Call set_currency in the postLogin hook, after the customer ID is set. I.e. call it a second time on the same page load. Although I'm not quite sure why it isn't already refreshing after login.
Hi @ecartz, any chance you could give me a bit more info on how to do this one, I can't work it out at all...

Re: Forcing Currency by Customer

Posted: Tue Oct 03, 2023 12:06 am
by Portman
This is what I have tried... in login.php.. it is not throwing up any errors, but it is also not working (ie I still need to refesh page)

Code: Select all

  require 'includes/application_top.php';
  global $currencies; //Added By Portman

// redirect the customer to a friendly cookie-must-be-enabled page if cookies are disabled (or the session has not started)
  if (!Session::is_started()) {
    if ( !isset($_GET['cookie_test']) ) {
      Href::redirect($Linker->build('login.php')->retain_query_except()->set_parameter('cookie_test', '1'));
    }

    Href::redirect($Linker->build('cookie_usage.php'));
  }

  // login content module must return $login_customer_id as an integer after successful customer authentication	
  $login_customer_id = false;
  $page_content = $Template->get_content('login');

  if ( is_int($login_customer_id) && ($login_customer_id > 0) ) {
    $hooks->cat('postLogin');
	$currencies->set_currency(); // Added By Portman
    Href::redirect($_SESSION['navigation']->pop_snapshot_as_link());
  }


  require language::map_to_translation('login.php');
  require $Template->map(__FILE__, 'page');
  require 'includes/application_bottom.php';
I have noticed in the error log that I am getting an "Undefined index" error or the variable wholesale_group on the following 2 lines of code from my currencies mod mentioned above;

Code: Select all

	  if (!isset($_SESSION['customer_id']) || ($_SESSION['wholesale_group'] == '0' )) {

Code: Select all

		else if ($_SESSION['wholesale_group'] > '0' ) {	
could this be the cause and if so what have I done wrong?

Re: Forcing Currency by Customer

Posted: Sat Oct 07, 2023 4:31 am
by Portman
Ok, I hope I am still getting some traction on this topic,

I have sorted out the errors above and I have started to work out the hooks thing and so have created a file in templates/override/includes/hooks/shop/sitewide/ReloadLogin.php, but i am still not sure how to call the set_currency() function... here is my code which obviously does not work...

Code: Select all

<?php

class hook_shop_siteWide_ReloadLogin {
	
	function listen_postLogin() {
	$currencies->set_currency();
	}
	
}	
?>	
Any thoughts on how I go about calling set_currency() from here would be really appreciated.