Skip to content

Instantly share code, notes, and snippets.

@dparker1005
Last active August 11, 2023 10:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dparker1005/6dacc12fc71a05038ab70b950a288c3a to your computer and use it in GitHub Desktop.
Save dparker1005/6dacc12fc71a05038ab70b950a288c3a to your computer and use it in GitHub Desktop.
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 );
@MaryOJob
Copy link

This adds the donation amount and membership amount only to the invoice and email invoice: https://gist.github.com/MaryOJob/87f5a7a4988647691d7d92f75fa7fe8e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment