Skip to content

Instantly share code, notes, and snippets.

@Pebblo
Last active March 9, 2023 12:05
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 Pebblo/dc011b43b461064a831d to your computer and use it in GitHub Desktop.
Save Pebblo/dc011b43b461064a831d to your computer and use it in GitHub Desktop.
Add a [PROMO_CODES_USED] shortcode to the EE messages, this shortcode outputs any promotion codes used within a transaction. If the Promo code is a coded promotion the format will be "Promo Name[{Promo_code}]" otherwise its use "Promo Name".
<?php
/*
Plugin Name: Event Espresso site specific functions
Description: Add custom functions for Event Espresso to this plugin.
/* Begin Adding Functions Below This Line; Do not include an opening PHP tag as this sample code already includes one! */
function tw_ee_register_new_promo_shortcode( $shortcodes, EE_Shortcodes $lib ) {
if ( $lib instanceof EE_Transaction_Shortcodes ) {
//Add your shortcode to the add as the key, the value should be a description of the shortcode.
$shortcodes['[PROMO_CODES_USED]'] = _('This shortcode outputs any Promotion codes used during registration.');
}
//Return the shortcodes.
return $shortcodes;
}
add_filter( 'FHEE__EE_Shortcodes__shortcodes', 'tw_ee_register_new_promo_shortcode', 10, 2 );
function tw_ee_register_new_promo_shortcode_parser( $parsed, $shortcode, $data, $extra_data, EE_Shortcodes $lib ) {
//Check we are within the EE_Transaction_Shortcodes
if ( $lib instanceof EE_Transaction_Shortcodes && $shortcode == '[PROMO_CODES_USED]' ) {
//Pull the transaction from the EE_Messages_Addressee object passed to parser.
$transaction = $data->txn;
//Check we have an EE_Transaction object
if ( $transaction instanceof EE_Transaction ) {
//Pull in the promotion line items for this transaction
$promo_rows = EEM_Price::instance()->get_all_wpdb_results(
array(
array(
'Promotion.Line_Item.TXN_ID' => $transaction->ID()
)
));
//Setup an arrey to store all promo codes used on the transaction
$promo_codes = array();
//Loop through promo line items and build the promo_codes array using the Promocode name and code (if available)
foreach( $promo_rows as $promo_row ) {
if( $promo_row[ 'Promotion.PRO_code' ] ) {
$promo_codes[] = sprintf( '%1$s [%2$s]', $promo_row[ 'Price.PRC_name' ], $promo_row[ 'Promotion.PRO_code'] );
}else{
$promo_codes[] = $promo_row[ 'Price.PRC_name' ];
}
}
//Implode the promo_codes array into a comma-delimited string.
$promo_codes = implode(',', $promo_codes );
//Return a single string or promo codes used.
return $promo_codes;
}
}
//If not within the correct section, or parsing the correct shortcode,
//Return the currently parsed content.
return $parsed;
}
add_filter( 'FHEE__EE_Shortcodes__parser_after', 'tw_ee_register_new_promo_shortcode_parser', 10, 5 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment