Created
February 4, 2014 15:59
-
-
Save strangerstudios/8806443 to your computer and use it in GitHub Desktop.
Change currencies depending on Paid Memberships Pro level. Add this code to your active theme's functions.php or a custom plugin. This is just an example that will need to be tweaked for your needs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Change currencies depending on Paid Memberships Pro level. | |
Add this code to your active theme's functions.php or a custom plugin. | |
This is just an example that will need to be tweaked for your needs. | |
Other places to look into swapping currencies: | |
* Levels page. | |
* Invoices page. | |
* In emails. | |
* In membership levels table in admin. | |
* When using discount codes. | |
*/ | |
/* | |
Global to store levels with non-default currencies | |
Keys are level ids. Values are an asrray with the currency abbreviation and symbol as the first and second entries. | |
*/ | |
global $level_currencies; | |
$level_currencies = array( | |
5 => array("EUR", "€"), | |
6 => array("GBP", "£") | |
); | |
//main function to check for a currency level and update currencies | |
function update_currency_per_level($level_id) | |
{ | |
global $pmpro_currency, $pmpro_currency_symbol, $level_currencies; | |
foreach($level_currencies as $level_currency_id => $level_currency) | |
{ | |
if($level_id == $level_currency_id) | |
{ | |
$pmpro_currency = $level_currency[0]; | |
$pmpro_currency_symbol = $level_currency[1]; | |
} | |
} | |
} | |
//change currency on checkout page | |
function my_pmpro_checkout_level($level) | |
{ | |
update_currency_per_level($level->id); | |
return $level; | |
} | |
add_filter("pmpro_checkout_level", "my_pmpro_checkout_level"); | |
//change currency when sent as a request param | |
function my_init_currency_check() | |
{ | |
if(!empty($_REQUEST['level'])) | |
return update_currency_per_level(intval($_REQUEST['level'])); | |
} | |
add_action("init", "my_init_currency_check"); | |
//params in the admin | |
function my_admin_init_currency_check() | |
{ | |
if(!empty($_REQUEST['edit']) && !empty($_REQUEST['page']) && $_REQUEST['page'] == 'pmpro-membershiplevels') | |
return update_currency_per_level(intval($_REQUEST['edit'])); | |
} | |
add_action("admin_init", "my_admin_init_currency_check"); |
Thanks Andrew.
Do you see any way to change the currency globally via a switcher?
Following on from this, this would be great if a switcher was an option in the future
Dear Team,
Please, I just need two forms of currency (NGN and USD) option for members to select from. How can this be implemented using the above script?
Is the modification below work?
global $level_currencies;
$level_currencies = array(
5 => array("NGN", "&naira;"),
6 => array("USD", "$")
Thank you.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This may require a bit more work to replace the currency in all locations, and there may be some limitations.