-
-
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. | |
| 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.
instead of this solution I prefer the way woocommerce switcher works it makes currency exchange rate more easy e.g., I have one level called 16 would make same level of setting currencies
` function my_african_countries() {
return array(
'DZ','AO','BR','BJ','BW','BF','BI','CM','CV','CF','TD','KM','CG','CD','CI',
'DJ','EG','GQ','ER','SZ','ET','GA','GM','GH','GN','GW','KE','LS','LR',
'LY','MG','MW','ML','MR','MU','MA','MZ','NA','NE','NG','RW','ST','SN',
'SC','SL','SO','ZA','SS','SD','TZ','TG','TN','UG','ZM','ZW'
);
}
function my_get_country_by_ip() {
$ip = WC_Geolocation::get_ip_address();
$geo = WC_Geolocation::geolocate_ip( $ip );
return ! empty( $geo['country'] ) ? $geo['country'] : '';
}
add_action( 'pmpro_membership_level_after_other_settings', 'pmpro_add_country_price_fields' );
function pmpro_add_country_price_fields() {
if ( ! isset( $_GET['edit'] ) ) return;
$level_id = intval( $_GET['edit'] );
?>
<h3 class="topborder">Country Specific Prices</h3>
<table class="form-table">
<tbody>
<?php foreach ( my_african_countries() as $country_code ) :
$key = 'pmpro_price_'.$country_code.'_'.$level_id;
?>
<tr>
<th scope="row"><?php echo esc_html( $country_code ); ?></th>
<td>
<input
type="number"
step="0.01"
name="<?php echo esc_attr( $key ); ?>"
value="<?php echo esc_attr( get_option( $key ) ); ?>"
/>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php
}
add_action( 'admin_init', 'pmpro_save_country_prices' );
function pmpro_save_country_prices() {
if ( ! isset( $_GET['edit'] ) ) return;
$level_id = intval( $_GET['edit'] );
foreach ( my_african_countries() as $country_code ) {
$key = 'pmpro_price_'.$country_code.'_'.$level_id;
if ( isset( $_REQUEST[ $key ] ) ) {
update_option(
$key,
sanitize_text_field( $_REQUEST[ $key ] )
);
}
}
}
add_filter( 'pmpro_checkout_level', 'pmpro_country_based_level_price', 10, 1 );
function pmpro_country_based_level_price( $level ) {
global $pmpro_currency, $pmpro_currency_symbol, $level_currencies;
$ip = WC_Geolocation::get_ip_address();
$geo = WC_Geolocation::geolocate_ip( $ip );
$country = $geo['country'];
// Only apply for African countries
if ( ! in_array( $country, my_african_countries(), true ) ) {
return $level;
}
// Option key (same pattern you already use)
$key = 'pmpro_price_' . $country . '_' . $level->id;
$custom_price = get_option( $key );
$currency = get_option( 'pmpro_currency', 'GBP' );
$rate = 1912.6;
if ( $custom_price !== '' ) {
// Initial payment
if ( ! empty( $level->initial_payment ) ) {
$level->initial_payment = round( $custom_price / $rate, 2 );
$pmpro_currency = 'NGN';
$pmpro_currency_symbol = '₦';
}
}
return $level;
}
add_filter( 'pmpro_level_cost_text', 'pmpro_country_price_cost_text', 10, 2 );
function pmpro_country_price_cost_text( $cost, $level ) {
global $pmpro_currency_symbol;
$ip = WC_Geolocation::get_ip_address();
$geo = WC_Geolocation::geolocate_ip( $ip );
$country = $geo['country'];
if ( ! in_array( $country, my_african_countries(), true ) ) {
return $cost;
}
$key = 'pmpro_price_' . $country . '_' . $level->id;
$custom_price = get_option( $key );
if ( $custom_price !== '' ) {
$symbol = $pmpro_currency_symbol; // default example
$cost = '<div class="pmpro_level_cost_text">
<p><strong>' . esc_html( $symbol . number_format( $custom_price, 2 ) ) . '</strong>
<small>(regional pricing applied)</small></p>
</div>';
}
return $cost;
} `
though this is not complete tested code this way we don't have to manage new level as customization with level id might be there in pmpro customization plugin
You can also try this plugin (maintained by a developer outside of PMPro) -> https://wordpress.org/plugins/pmpro-multiple-currencies/
this plugin also have same issue we need to create new level the solution which i gave is more wise then creating new level the whole idea is to maintain different price as per the currency at same level id
@andrewlimaza whole idea here is if the geo location is Nigeria then charge as per Naira and if the geo location is Liverpool suppose then charge as GBP
I can improvise it further once my all test goes perfect @andrewlimaza but solution that you gave is not much helpful
This may require a bit more work to replace the currency in all locations, and there may be some limitations.