Skip to content

Instantly share code, notes, and snippets.

@messica
Last active March 25, 2022 16:00
Show Gist options
  • Save messica/33c769db1267f7aaf3741f1cf0989de2 to your computer and use it in GitHub Desktop.
Save messica/33c769db1267f7aaf3741f1cf0989de2 to your computer and use it in GitHub Desktop.
Display original price and discount when a discount code is applied.
<?php
/**
* Display messages of the Original Price, Discounted Price and Amount Saved when discount code is applied to PMPro order.
* Add this code recipe to a PMPro Customization Plugin - Display messages of the Original Price, Discounted Price and Amount Saved when discount code is applied to PMPro order
* Various classes added to strings to allow for styling - ".pmpro-discorig-message", ".pmpro-orginal-price", ".pmpro-discount-price", ".pmpro-save-price"
*
* [my_pmpro_applydiscountcode_return_js] Display original price and discount when a discount code is applied.
* @param string $discount_code [description]
* @param integer $discount_code_id [description]
* @param integer $level_id [description]
* @param integer $code_level [description]
* @return void
*/
function my_pmpro_applydiscountcode_return_js( $discount_code, $discount_code_id, $level_id, $code_level ) {
// Only continue if code is valid.
if( false == $code_level ) return;
// Get the original level.
$level = pmpro_getLevel( $level_id );
// Format prices.
$original_price = pmpro_formatPrice( $level->initial_payment );
$discounted_price = pmpro_formatPrice( $code_level->initial_payment );
$discount = $level->initial_payment - $code_level->initial_payment;
$discount = pmpro_formatPrice( $discount );
// Build HTML.
$html = "'<div class=\"pmpro-discorig-message pmpro-original-price\">The original price is {$original_price}. </div>";
$html .= "<div class=\"pmpro-discorig-message pmpro-discount-price\">The discounted price is {$discounted_price}. </div>";
$html .= "<div class=\"pmpro-discorig-message pmpro-save-price\">You save {$discount}.</div>'";
?>
jQuery("#pmpro_level_cost").html(<?php echo $html; ?>);
<?php
}
add_action( 'pmpro_applydiscountcode_return_js', 'my_pmpro_applydiscountcode_return_js', 10, 4);
@laurenhagan0306
Copy link

This recipe is included in the blog post on "Display the original and discounted price when a discount code is applied at checkout." at Paid Memberships Pro here: https://www.paidmembershipspro.com/display-the-original-and-discounted-price-when-a-discount-code-is-applied-at-checkout/

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