Skip to content

Instantly share code, notes, and snippets.

@wpweb101
Created January 11, 2018 14:07
Show Gist options
  • Save wpweb101/b216abd184ecf66d072bce984422c1a6 to your computer and use it in GitHub Desktop.
Save wpweb101/b216abd184ecf66d072bce984422c1a6 to your computer and use it in GitHub Desktop.
Edd Currency Price Format.
<?php
function edd_currency_csmz_price_format($newpriceformat, $price, $currency, $formatted){
global $edd_options, $post, $edd_currency_public, $edd_currency_model;
if(!empty($post) && $post->post_type == 'download') {
// If replace currency code
if( isset($edd_options['replace_currency_code']) && $edd_options['replace_currency_code'] == '1' ) {
// Getting main plugin currency position
if( isset($edd_options['currency_position']) && $edd_options['currency_position'] == 'before' ) {
$newpriceformat = '<span class="currency-symbol">'.$currency.'</span> <span class="currency-amount">'.$price.'</span>';
} else {
$newpriceformat = '<span class="currency-amount">'.$price.'</span> <span class="currency-symbol">'.$currency.'</span>';
}
} elseif( isset( $edd_options['append_code'] ) && $edd_options['append_code'] == '1' ) { //Check Append Currency Code settings is checked
//check append currency code is before
if( isset( $edd_options['curr_code_position'] ) && $edd_options['curr_code_position'] == 'before' ) {
$newpriceformat = '<span class="currency-symbol">'.$currency.'</span> <span class="currency-amount">'.$newpriceformat.'</span>';
} else {
$newpriceformat = '<span class="currency-amount">'.$newpriceformat.'</span> <span class="currency-amount">'.$currency.'</span>';
}
} //end if to check append code is enabled or not
}
return $newpriceformat;
}
add_filter('edd_currency_price_format', 'edd_currency_csmz_price_format', 10, 4);
function edd_currency_csmz_change_before_format($new_formatted, $formatted, $currency, $price){
global $edd_options, $post, $edd_currency_public, $edd_currency_model;
if(!empty($post) && $post->post_type == 'download') {
$stored_currency = edd_currency_get_stored_currency(); // get cookie currency
$exchange_rates = edd_currency_get_exchange_rates();
$cur_symbol = edd_currency_get_symbol( $currency ) ;
$new_formatted = $edd_currency_public->edd_currency_append_code( $cur_symbol, $price, $currency, $formatted );
if( !empty( $price ) && $stored_currency != $currency && $edd_currency_model->edd_currency_check_open_exchange() && isset( $exchange_rates[$stored_currency] ) ) {
// check if cookie curreny is not the same as base currency and open change rate is setup properly and custom rate is set for stored currency
$new_symbol = edd_currency_get_symbol( $stored_currency );
$new_price = edd_currency_get_converted_price( $price );
$new_price_formt = $new_symbol.$new_price;
$new_changed_format = $edd_currency_public->edd_currency_append_code( $new_symbol, $new_price, $stored_currency, $new_price_formt );
if( isset( $edd_options['replace_base_currency'] ) && $edd_options['replace_base_currency'] == '1' ) {
$new_formatted = $new_changed_format;
} else {
$new_formatted .= ' <span class="main-wrap">( ' . $new_changed_format . ' )</span> ' ;
}
}
}
return $new_formatted;
}
add_filter('edd_currency_change_before_format', 'edd_currency_csmz_change_before_format', 10, 4);
function edd_currency_csmz_change_after_format($new_formatted, $formatted, $currency, $price){
global $edd_options, $post, $edd_currency_public, $edd_currency_model;
if(!empty($post) && $post->post_type == 'download') {
$stored_currency = edd_currency_get_stored_currency();
$exchange_rates = edd_currency_get_exchange_rates();
$cur_symbol = edd_currency_get_symbol( $currency ) ;
$new_formatted = $edd_currency_public->edd_currency_append_code( $cur_symbol, $price, $currency, $formatted );
//Check Our Cookie is set
if( !empty( $price ) && $stored_currency != $currency && $edd_currency_model->edd_currency_check_open_exchange() && isset( $exchange_rates[$stored_currency] ) ) {
$new_symbol = edd_currency_get_symbol( $stored_currency );
$new_price = edd_currency_get_converted_price( $price );
// only 1 line change , we can make this functions ( edd_currency_change_after_format and edd_currency_change_before_format) common in future
$new_price_formt = $new_price.$new_symbol;
$new_changed_format = $edd_currency_public->edd_currency_append_code( $new_symbol, $new_price, $stored_currency, $new_price_formt );
//Check Replace Base Currency settings is checked
if( isset( $edd_options['replace_base_currency'] ) && $edd_options['replace_base_currency'] == '1' ) {
$new_formatted = $new_changed_format;
} else {
$new_formatted .= ' <span class="main-wrap">( ' . $new_changed_format . ' )</span> ';
}
}
}
return $new_formatted;
}
add_filter('edd_currency_change_after_format', 'edd_currency_csmz_change_after_format', 10, 4);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment