Skip to content

Instantly share code, notes, and snippets.

@dparker1005
Last active April 7, 2023 15:37
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 dparker1005/b435711fac10cc64ae01bb36724fdd11 to your computer and use it in GitHub Desktop.
Save dparker1005/b435711fac10cc64ae01bb36724fdd11 to your computer and use it in GitHub Desktop.
Checks if a certain discount code was used in the checkout and shows messages in multiple places.
<?php
// Copy from below here...
/**
* Checks if a certain discount code was used in the checkout and:
* - Shows a special message on the Confirmation page
* - Includes a special message in the Confirmation email
* - Shows a message on the Invoice for that order/checkout
* - Shows a message on the Account page if the most recent order used the code in the past week
*/
/**
* Get the discount code message to show for a given order.
*
* @param MemberOrder $order The order to get the message for.
* @return string The message to show.
*/
function pmprodiscountmessage_get_message_for_order( $order ) {
// Make sure that we have an order.
if ( empty( $order ) ) {
return '';
}
// Get the discount code used for this order.
$discount_code = $order->getDiscountCode();
// Make sure that we have a discount code.
if ( empty( $discount_code ) || empty( $discount_code->code ) ) {
return '';
}
// Switch on the code property of the discount code to show a message per code.
// Edit these cases to set messages per discount code.
switch ( $discount_code->code ) {
case 'my-special-code':
return 'Thanks for using my special code!';
break;
case 'my-other-special-code':
return 'Thanks for using my other special code!';
break;
case 'free':
return 'Thanks for using the code on my test site!';
break;
case 'my-super-secret-code-1':
case 'my-super-secret-code-2':
case 'my-super-secret-code-3':
return 'Thanks for using one of my super secret codes!';
break;
}
return '';
}
/**
* Show a message on the Confirmation page if a certain discount code was used at checkout.
*
* It would be nice to use the $order parameter for the filter, but it is only passed for
* paid checkouts so it wouldn't work for discount codes that make the checkout free.
*
* @param string $message The current confirmation message.
* @return string The new confirmation message.
*/
function pmprodiscountmessage_confirmation_message( $message ) {
global $current_user;
// Get the most recent order for this user.
$order_args = array(
'user_id' => $current_user->ID,
);
$order = MemberOrder::get_order( $order_args );
// Make sure that we have an order.
$discount_message = pmprodiscountmessage_get_message_for_order( $order );
if ( ! empty( $discount_message ) ) {
$message .= '<p>' . $discount_message . '</p>';
}
return $message;
}
add_filter( 'pmpro_confirmation_message', 'pmprodiscountmessage_confirmation_message' );
/**
* Add a !!discount_message!! email template variable that shows a message if a certain
* discount code was used at checkout in the Confirmation email.
*
* @param array $data The email template variable data.
* @param PPMroEmail $email The email object.
* @return array The new email template variable data.
*/
function pmprodiscountmessage_email_data( $data, $email ) {
// Check if the email template begins with `checkout_`.
if ( 0 !== strpos( $email->template, 'checkout_' ) ) {
return $data;
}
// Add a default !!discount_message!! email template variable just in case.
$data['discount_message'] = '';
// Get the user from the !!user_login!! email template variable.
if ( empty( $data['user_login'] ) ) {
return $data;
}
$user = get_user_by( 'login', $data['user_login'] );
if ( empty( $user ) ) {
return $data;
}
// Get the most recent order for this user.
$order_args = array(
'user_id' => $user->ID,
);
$order = MemberOrder::get_order( $order_args );
$discount_message = pmprodiscountmessage_get_message_for_order( $order );
if ( ! empty( $discount_message ) ) {
$data['discount_message'] = $discount_message;
}
return $data;
}
add_filter( 'pmpro_email_data', 'pmprodiscountmessage_email_data', 10, 2 );
/**
* Show an invoice bullet on the Invoice page if a certain discount code was used for that order.
*
* @param MemberOrder $order The order to show the invoice bullet for.
*/
function pmprodiscountmessage_invoice_bullets_bottom( $order ) {
global $pmpro_pages;
// Make sure that we are on the Invoice page. We are alredy updating the message
// on the Confirmation page so we don't want to add a bullet there.
if ( empty( $pmpro_pages['invoice'] ) || ! is_page( $pmpro_pages['invoice'] ) ) {
return;
}
// Get the discount code message for this order.
$discount_message = pmprodiscountmessage_get_message_for_order( $order );
if ( empty( $discount_message ) ) {
return;
}
// Show the message as an invoice bullet.
?>
<li>
<?php echo esc_html( $discount_message ); ?>
</li>
<?php
}
add_action( 'pmpro_invoice_bullets_bottom', 'pmprodiscountmessage_invoice_bullets_bottom' );
/**
* Show a message in account bullets if the user has used a certain discount code in the past
* week and still has the associated membership level.
*
* Note: Will only show a message for the user's most recent order, so may not be the expected behavior
* for all MMPU use-cases.
*/
function pmprodiscountmessage_account_bullets_bottom() {
global $current_user;
// Get the most recent order for this user.
$order_args = array(
'user_id' => $current_user->ID,
);
$order = MemberOrder::get_order( $order_args );
// Make sure that we have an order.
if ( empty( $order ) ) {
return;
}
// Check whether the user still has this order's level.
if ( empty( $order->membership_id ) || ! pmpro_hasMembershipLevel( $order->membership_id, $current_user->ID ) ) {
return;
}
// Check whether the order was placed within the last week.
$week_ago = time() - WEEK_IN_SECONDS;
if ( $order->timestamp < $week_ago ) {
return;
}
// Get the discount code message for this order.
$discount_message = pmprodiscountmessage_get_message_for_order( $order );
if ( empty( $discount_message ) ) {
return;
}
// Show the message as an account bullet.
?>
<li>
<?php echo esc_html( $discount_message ); ?>
</li>
<?php
}
add_action( 'pmpro_account_bullets_bottom', 'pmprodiscountmessage_account_bullets_bottom' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment