Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mircobabini/57709290c42b1eeded877edc38dd2fca to your computer and use it in GitHub Desktop.
Save mircobabini/57709290c42b1eeded877edc38dd2fca to your computer and use it in GitHub Desktop.
<?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 );
/**
* [my_pmpro_checkout_after_level_cost] 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_checkout_after_level_cost() {
global $wpdb, $discount_code, $pmpro_level;
if ( ! empty( $_REQUEST['discount_code'] ) ) {
$discount_code = preg_replace( "/[^A-Za-z0-9\-]/", "", $_REQUEST['discount_code'] );
} else {
if ( ! $discount_code ) {
return;
}
}
$discount_code_id = $wpdb->get_var( "SELECT id FROM $wpdb->pmpro_discount_codes WHERE code = '" . $discount_code . "' LIMIT 1" );
if ( ! empty( $_REQUEST['level'] ) ) {
$level_str = $_REQUEST['level'];
$level_str = str_replace( ' ', '+', $level_str ); // If val passed via URL, + would be converted to space.
$level_ids = array_map( 'intval', explode( '+', $level_str ) );
} else {
$level_ids = [ $pmpro_level->id ];
}
// Okay, send back new price info.
// Find levels whose price this code changed...
$sqlQuery = "
SELECT l.id, cl.*, l.name, l.description, l.allow_signups
FROM $wpdb->pmpro_discount_codes_levels cl
LEFT JOIN $wpdb->pmpro_membership_levels l
ON cl.level_id = l.id
LEFT JOIN $wpdb->pmpro_discount_codes dc
ON dc.id = cl.code_id WHERE dc.code = '" . $discount_code . "'
AND cl.level_id IN (" . implode( ',', $level_ids ) . ")";
$code_levels = $wpdb->get_results( $sqlQuery );
// ... and then get prices for the remaining levels.
$levels_found = array();
foreach ( $code_levels as $code_level ) {
$levels_found[] = intval( $code_level->level_id );
}
if ( ! empty( array_diff( $level_ids, $levels_found ) ) ) {
$sqlQuery = "SELECT * FROM $wpdb->pmpro_membership_levels WHERE id IN (" . implode( ',', array_diff( $level_ids, $levels_found ) ) . ")";
$code_levels = array_merge( $code_levels, $wpdb->get_results( $sqlQuery ) );
}
//filter adjustments to the level
if ( count( $code_levels ) <= 1 ) {
// Should return just a single level object or null.
$code_levels = array( apply_filters( "pmpro_discount_code_level", empty( $code_levels ) ? null : $code_levels[0], $discount_code_id ) );
} else {
// Should return an array of levels objects.
$code_levels = apply_filters( "pmpro_discount_code_level", $code_levels, $discount_code_id );
}
// Only continue if code is valid.
if ( empty( $code_levels ) ) {
return;
}
$code_level = reset( $code_levels );
// Get the original level.
$level = pmpro_getLevel( reset( $level_ids ) );
// 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>'";
?>
<script>
jQuery("#pmpro_level_cost").html(<?php echo $html; ?>);
</script>
<?php
}
add_action( 'pmpro_checkout_after_level_cost', 'my_pmpro_checkout_after_level_cost' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment