News:

You may pay someone to create your store, or you visit our seminar and become a professional yourself with the silver certification

Main Menu

Change currency and country progmatically

Started by sandomatyas, April 26, 2019, 10:09:46 AM

Previous topic - Next topic

sandomatyas

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?

GJC Web Design

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?
GJC Web Design
VirtueMart and Joomla Developers - php developers https://www.gjcwebdesign.com
VM4 AusPost Shipping Plugin - e-go Shipping Plugin - VM4 Postcode Shipping Plugin - Radius Shipping Plugin - VM4 NZ Post Shipping Plugin - AusPost Estimator
Samport Payment Plugin - EcomMerchant Payment Plugin - ccBill payment Plugin
VM2 Product Lock Extension - VM2 Preconfig Adresses Extension - TaxCloud USA Taxes Plugin - Virtuemart  Product Review Component
https://extensions.joomla.org/profile/profile/details/67210
Contact for any VirtueMart or Joomla development & customisation

sandomatyas

Thanks. Meanwhile I was able to set the currency but not the country. Any idea?

GJC Web Design

no idea what u mean... set a country? what is set a country????? where?

GJC Web Design
VirtueMart and Joomla Developers - php developers https://www.gjcwebdesign.com
VM4 AusPost Shipping Plugin - e-go Shipping Plugin - VM4 Postcode Shipping Plugin - Radius Shipping Plugin - VM4 NZ Post Shipping Plugin - AusPost Estimator
Samport Payment Plugin - EcomMerchant Payment Plugin - ccBill payment Plugin
VM2 Product Lock Extension - VM2 Preconfig Adresses Extension - TaxCloud USA Taxes Plugin - Virtuemart  Product Review Component
https://extensions.joomla.org/profile/profile/details/67210
Contact for any VirtueMart or Joomla development & customisation

sandomatyas

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.

Milbo

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.
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/