Author Topic: Change currency and country progmatically  (Read 592 times)

sandomatyas

  • Jr. Member
  • **
  • Posts: 282
Change currency and country progmatically
« 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?

GJC Web Design

  • 3rd party VirtueMart Developer
  • Super Hero
  • *
  • Posts: 9200
  • Virtuemart, Joomla & php developer
    • GJC Web Design
  • VirtueMart Version: 3.6.8
Re: Change currency and country progmatically
« Reply #1 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



Code: [Select]
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 http://www.gjcwebdesign.com
VM3 AusPost Shipping Plugin - e-go Shipping Plugin - VM3 Postcode Shipping Plugin - Radius Shipping Plugin - VM3 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
http://extensions.joomla.org/profile/profile/details/67210
Contact for any VirtueMart or Joomla development & customisation

sandomatyas

  • Jr. Member
  • **
  • Posts: 282
Re: Change currency and country progmatically
« Reply #2 on: April 26, 2019, 11:52:45 am »
Thanks. Meanwhile I was able to set the currency but not the country. Any idea?

GJC Web Design

  • 3rd party VirtueMart Developer
  • Super Hero
  • *
  • Posts: 9200
  • Virtuemart, Joomla & php developer
    • GJC Web Design
  • VirtueMart Version: 3.6.8
Re: Change currency and country progmatically
« Reply #3 on: April 26, 2019, 11:54:59 am »
no idea what u mean... set a country? what is set a country????? where?

GJC Web Design
VirtueMart and Joomla Developers - php developers http://www.gjcwebdesign.com
VM3 AusPost Shipping Plugin - e-go Shipping Plugin - VM3 Postcode Shipping Plugin - Radius Shipping Plugin - VM3 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
http://extensions.joomla.org/profile/profile/details/67210
Contact for any VirtueMart or Joomla development & customisation

sandomatyas

  • Jr. Member
  • **
  • Posts: 282
Re: Change currency and country progmatically
« Reply #4 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.

Milbo

  • Virtuemart Projectleader
  • Administrator
  • Super Hero
  • *
  • Posts: 9973
  • VM3.2 Cached and Optimized
    • VM3 Extensions
  • VirtueMart Version: VirtueMart 3 on joomla 3
Re: Change currency and country progmatically
« Reply #5 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.
I should fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/