Last active
January 31, 2024 19:35
-
-
Save MaximilianoRicoTabo/9b5d4b16daa050dbc90b8b2e45c4087f to your computer and use it in GitHub Desktop.
Remove default level cost text and replace with a custom one in order to format price based on level.
This file contains hidden or 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 | |
/** | |
* 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. | |
*/ | |
/** | |
* Remove fee column and add a custom one to the Members List to show the multi currency level cost. | |
* | |
* @param array $columns columns to be displayed. | |
*/ | |
function my_memberslist_columns( $columns ) { | |
unset( $columns['fee'] ); | |
$columns['custom_fee'] = 'Level Cost Text'; | |
return $columns; | |
} | |
add_filter( 'pmpro_manage_memberslist_columns', 'my_memberslist_columns'); | |
/** | |
* | |
* Add the custom (currency formatted) fee to the members list. | |
* | |
* @param string $colname column being filled. | |
* @param string $item_id to get information for. | |
*/ | |
function my_pmpro_fill_memberslist_col_company( $colname, $item_id ) { | |
if ( 'custom_fee' === $colname ) { | |
global $wpdb; | |
$item = $wpdb->get_row( "SELECT * FROM $wpdb->pmpro_memberships_users WHERE user_id = $item_id ORDER BY id DESC LIMIT 1" ); | |
// If there is no payment for the level, show a dash. | |
if ( empty( $item ) || (float)$item->initial_payment <= 0 && (float)$item->billing_amount <= 0 ) { | |
echo esc_html__( '—', 'paid-memberships-pro' ); | |
} | |
$fee = ''; | |
// Display the member's initial payment. | |
if ( (float)$item->initial_payment > 0 ) { | |
$fee .= pmpro_escape_price( pmpro_custom_formatPrice( $item->initial_payment, $item->membership_id ) ); | |
} | |
// If there is a recurring payment, show a plus sign. | |
if ( (float) $item->initial_payment > 0 && (float)$item->billing_amount > 0 ) { | |
$fee .= esc_html__( ' + ', 'paid-memberships-pro' ); | |
} | |
// If there is a recurring payment, show the recurring payment amount and cycle. | |
if ( (float)$item->billing_amount > 0 ) { | |
$fee .= pmpro_escape_price( pmpro_custom_formatPrice( $item->billing_amount, $item->membership_id ) ); | |
$fee .= esc_html__( ' per ', 'paid-memberships-pro' ); | |
if ( $item->cycle_number > 1 ) { | |
$fee .= $item->cycle_number . " " . pmpro_translate_billing_period( $item->cycle_period, $item->cycle_number ); | |
} else { | |
$fee .= pmpro_translate_billing_period( $item->cycle_period, 1 ); | |
} | |
} | |
echo $fee; | |
} | |
} | |
add_filter( 'pmpro_manage_memberslist_custom_column', 'my_pmpro_fill_memberslist_col_company', 10, 2 ); | |
/** | |
* Add a custom format for the price of the level. | |
* | |
* @param string $price price to be formatted. | |
* @param int $level_id level id to get information for. | |
*/ | |
function pmpro_custom_formatPrice($price, $level_id) { | |
$formatted = pmpro_formatPrice($price); | |
//Level 3 return dollars, all the rest euros. Change this logic as needed. | |
if ( $level_id != 3 ) { | |
return str_replace( '$', '€', $formatted ); | |
} | |
return $formatted; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment