Last active
August 11, 2023 10:56
-
-
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.
This file contains 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 | |
// 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
This adds the donation amount and membership amount only to the invoice and email invoice: https://gist.github.com/MaryOJob/87f5a7a4988647691d7d92f75fa7fe8e