Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Automatically updates the cost of a membership level to be the local currency amount equivalent to a defined price in a different currency.
<?php
// Copy from below here...
/**
* Automatically updates the cost of a membership level to be the local currency amount
* equivalent to a defined price in a different currency.
*
* For example, the default setup below will get the conversion rate from USD to ZAR
* every day and use that information to set the membership price for level ID 5
* to be equivalent to $25 USD in ZAR. Information about this conversion can be
* displayed to the user using new variables in the PMPro Custom Level Cost Text Add On.
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
/*
* SETUP STEPS
*/
// Step 1: Go to https://free.currencyconverterapi.com and click "Get Your Free API Key".
// Step 2: Follow process to confirm email address and retrieve API key.
// Step 3: Enter API key below:
define( 'MY_PMPRO_CURRECNCYCONVERTER_API_KEY', 'XXXXXXXXXXXXXXXXXX' );
// Step 4: Create $my_pmpro_base_prices to set the "base prices" for your membership level.
global $my_pmpro_base_prices, $my_pmpro_currencyconverter_api_key;
$my_pmpro_base_prices = array(
5 => array( // Update price for level ID 5.
'currency' => 'USD', // Currency that you would like to convert from.
'initial_payment' => 25, // Initial payment amount in base currency
'billing_amount' => 25 // Recurring payment amount in base currency.
)
);
// Step 5: Run the code to update your prices by going to yoursite.com/wp-admin/?my_pmpro_update_currency_conversions=1.
// Step 6: Make sure that membership level prices were updated correctly.
// Step 7: (optional) Install PMPro Custom Level Cost Text add on and use new variables listed in my_pclct_variables_currency_conversions().
/*
* HANDLE UPDATING CURRENCIES
*/
function my_pmpro_update_currency_conversions() {
global $my_pmpro_base_prices, $my_pmpro_currencyconverter_api_key, $pmpro_currency;
$conversion_rate_cache = array();
foreach ( $my_pmpro_base_prices as $level_id => $base_price_data ) {
$level = new PMPro_Membership_Level( $level_id );
if ( empty( $level ) ) {
// Invalid level ID.
continue;
}
$base_price_data['currency'] = strtoupper( $base_price_data['currency'] );
if ( ! isset( $conversion_rate_cache[ $base_price_data['currency'] ] ) && defined( 'MY_PMPRO_CURRECNCYCONVERTER_API_KEY' ) ) {
// We have not yet checked this currency's conversion rate. Do it now.
$args = array(
'apiKey' => MY_PMPRO_CURRECNCYCONVERTER_API_KEY,
'q' => $base_price_data['currency'] . '_' . strtoupper( $pmpro_currency ),
);
$conversion_data_raw = wp_remote_get( add_query_arg( $args, 'https://free.currconv.com/api/v7/convert' ) );
$conversion_data_body = json_decode( $conversion_data_raw['body'] );
$conversion_rate = $conversion_data_body->results->{$args['q']}->val;
$conversion_rate_cache[ $base_price_data['currency'] ] = $conversion_rate;
}
if ( empty( $conversion_rate_cache[ $base_price_data['currency'] ] ) ) {
// Recieved an error from API call. Clear out old conversion rate.
delete_pmpro_membership_level_meta( $level_id, 'conversion_rate' );
continue;
}
$level->initial_payment = pmpro_round_price( $base_price_data['initial_payment'] * $conversion_rate_cache[ $base_price_data['currency'] ] );
$level->billing_amount = pmpro_round_price( $base_price_data['billing_amount'] * $conversion_rate_cache[ $base_price_data['currency'] ] );
$level->save();
update_pmpro_membership_level_meta( $level_id, 'conversion_rate', $conversion_rate_cache[ $base_price_data['currency'] ] );
}
}
add_action( 'pmpro_cron_expire_memberships', 'my_pmpro_update_currency_conversions' );
function my_pmpro_update_currency_conversions_admin_init() {
if ( isset( $_REQUEST['my_pmpro_update_currency_conversions'] ) ) {
my_pmpro_update_currency_conversions();
}
}
add_action( 'admin_init', 'my_pmpro_update_currency_conversions_admin_init' );
/*
* INTEGRATE WITH PMPRO CUSTOM LEVEL COST TEXT
*/
function my_pclct_variables_currency_conversions( $search ) {
$search[] = '!!site_currency!!';
$search[] = '!!base_price!!';
$search[] = '!!base_currency!!';
$search[] = '!!conversion_rate!!';
return $search;
}
add_filter( 'pclct_variables', 'my_pclct_variables_currency_conversions' );
function my_pclct_content_currency_conversions( $replace, $cost, $level ) {
global $my_pmpro_base_prices, $pmpro_currency;
$replace[] = $pmpro_currency;
$initial_payment = $my_pmpro_base_prices[ $level->id ]['initial_payment'];
$replace[] = empty( $initial_payment ) ? '???' : $initial_payment;
$base_currency = $my_pmpro_base_prices[ $level->id ]['currency'];
$replace[] = empty( $base_currency ) ? '???' : $base_currency;
$conversion_rate = get_pmpro_membership_level_meta( $level->id, 'conversion_rate', true );
$replace[] = empty( $conversion_rate ) ? '???' : $conversion_rate;
return $replace;
}
add_filter( 'pclct_variables_content', 'my_pclct_content_currency_conversions', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment