VirtueMart Forum

VirtueMart 2 + 3 + 4 => Virtuemart Development and bug reports => Topic started by: sandomatyas on April 26, 2019, 10:09:46 AM

Title: Change currency and country progmatically
Post by: sandomatyas on April 26, 2019, 10:09:46 AM
Sometimes I need to change the shop's currency and country progmatically. Like when I get a specified URL parameter. I can get it in a system plugin's onAfterRoute method but how should I set the currency and the country?
Title: Re: Change currency and country progmatically
Post by: GJC Web Design on April 26, 2019, 11:46:16 AM
I do something like this for currency
can be in a plugin or module
you need to call the function somehow based on what your trying to do



public static function setCurrency($req_currency){
        $app = JFactory::getApplication();
$need_change_cur = $app->getUserState('virtuemart_currency_id');

if (!class_exists('VmConfig'))
  require(JPATH_ADMINISTRATOR.DIRECTORY_SEPARATOR.'components'.DIRECTORY_SEPARATOR.'com_virtuemart'.DIRECTORY_SEPARATOR.'helpers'.DIRECTORY_SEPARATOR.'config.php');
  VmConfig::loadConfig(true);

// set global request variable
JRequest::setVar('virtuemart_currency_id', $req_currency);
 
$current_cur = $app->getUserState('virtuemart_currency_id');
//if same return
if($current_cur == $req_currency ){

return $need_change_cur;
}
$app->setUserState('virtuemart_currency_id', $req_currency);
$app->setUserState('com_virtuemart.virtuemart_currency_id', $req_currency);

if (!class_exists('VirtuemartCart'))
require(JPATH_SITE.DIRECTORY_SEPARATOR.'components'.DIRECTORY_SEPARATOR.'com_virtuemart'.DIRECTORY_SEPARATOR.'helpers'.DIRECTORY_SEPARATOR.'cart.php');
$cart = VirtuemartCart::getCart();
$cart->pricesCurrency = $req_currency;

return;
}


might be a bit old now and maybe see how the currency module does it

by country no idea what u mean... set a country?
Title: Re: Change currency and country progmatically
Post by: sandomatyas on April 26, 2019, 11:52:45 AM
Thanks. Meanwhile I was able to set the currency but not the country. Any idea?
Title: Re: Change currency and country progmatically
Post by: GJC Web Design on April 26, 2019, 11:54:59 AM
no idea what u mean... set a country? what is set a country????? where?

Title: Re: Change currency and country progmatically
Post by: sandomatyas on April 26, 2019, 14:48:39 PM
The shop shipps to 2 different countries which have different currencies. The site owner wants to let the customer select the currency and bind the cart's virtuemart_country_id to this selection. Like USD must be the USA, JPY must be Japan. The customer cant't change the country (I've already made it readonly) but need to change id depending the selected currency.
Title: Re: Change currency and country progmatically
Post by: Milbo on May 15, 2019, 18:59:48 PM
I suggest to buy this plugin https://extensions.virtuemart.net/shopper-order/orders/vm-localise-detail

There is a trigger which shows exactly what you want, but commented. With 3 if cases, you just need to adjust the country and currency. The reason is, that I just had not the time to write a nice gui for configuring the mapping of countries to currencies. But I am sure you can edit it within 10 minutes the way you need it.

Using the plugin the whole process is automatic. The user has not to select anything, the country is recognised by IP.