Forked from strangerstudios/update_currency_per_level.php
Last active
April 27, 2020 09:21
-
-
Save MaryOJob/d28fe49ea0d07371608f3bbc82264725 to your computer and use it in GitHub Desktop.
Change currencies depending on Paid Memberships Pro level from your default to Rand
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
<?php // DO NOT COPY THIS LINE PLEASE | |
/* | |
Change currencies depending on Paid Memberships Pro level. | |
Add this code to your active theme's functions.php or a custom plugin. | |
This changes currency for a membership level from the default currency on your site to Rand. | |
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( | |
3 => array("ZAR", "R"), // Where 3 should be replaced with the ID of your membership level | |
); | |
//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"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment