Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of how to use pmpro_get_price_parts filter alongside PMPro Donations with custom "extras" included in price.
<?php
// Copy from below here...
/**
* Example of how to use pmpro_get_price_parts filter alongside PMPro Donations
* with custom "extras" included in price.
*
* 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/
*/
function my_custom_get_price_parts( $price_parts, $invoice ) {
if ( ! function_exists( 'pmprodon_get_price_components' ) ) {
// PMPro donations not installed.
return;
}
$donation_price_components = pmprodon_get_price_components( $invoice );
$add_on_1_amount = floatval(1); // Get dynamically from order.
$add_on_2_amount = floatval(2); // Get dynamically from order.
$subtotal = $invoice->total - $add_on_1_amount - $add_on_2_amount;
$donation_amount = floatval( $donation_price_components['donation'] );
$membership_amount = $subtotal - $donation_amount;
$price_parts[ 'subtotal' ] = array(
'label' => 'Subtotal',
'value' => pmpro_escape_price( pmpro_formatPrice( $subtotal ) ),
);
$price_parts[ 'sub_element_membership' ] = array(
'label' => 'Membership Price',
'value' => pmpro_escape_price( pmpro_formatPrice( $membership_amount ) ),
);
$price_parts[ 'sub_element_donation' ] = array(
'label' => 'Donation',
'value' => pmpro_escape_price( pmpro_formatPrice( $donation_amount ) ),
);
$price_parts[ 'add_on_1' ] = array(
'label' => 'Add On 2',
'value' => pmpro_escape_price( pmpro_formatPrice( $add_on_1_amount ) ),
);
$price_parts[ 'add_on_2' ] = array(
'label' => 'Add On 1',
'value' => pmpro_escape_price( pmpro_formatPrice( $add_on_2_amount ) ),
);
return $price_parts;
}
add_filter( 'pmpro_get_price_parts', 'my_custom_get_price_parts', 10, 2 );
function my_custom_price_part_element_class( $class, $element ) {
if ( substr( $element, 0, 28 ) === "pmpro_price_part-sub_element" ) {
$class[]= 'pmpro_price_part_sub';
}
return $class;
}
add_filter( 'pmpro_element_class', 'my_custom_price_part_element_class', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment