Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save greathmaster/a11fffd0a98fc7c6f6f0aff5921812e8 to your computer and use it in GitHub Desktop.
Save greathmaster/a11fffd0a98fc7c6f6f0aff5921812e8 to your computer and use it in GitHub Desktop.
Creates a PMPro email shortcode for the sponsored members message to use for greater control over location of message. Use !!sponsored_message!! in email checkout body to display. Removes the automatic insertion of message at the top of the email.
/*Creates a PMPro email shortcode for the sponsored members message to use for greater control over location of message.
Use !!sponsored_message!! in email checkout body to display.
Removes the automatic insertion of message at the top of the email.
*/
function my_sponsored_email_shortcode($pmpro_email)
{
global $wpdb, $pmprosm_sponsored_account_levels;
$user_id = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_email = '" . $pmpro_email->data['user_email'] . "' LIMIT 1");
$level_id = $pmpro_email->data['membership_id'];
$code_id = pmprosm_getCodeByUserID($user_id);
if(!empty($user_id) && !empty($code_id) && pmprosm_isMainLevel($level_id))
{
$code = $wpdb->get_row("SELECT * FROM $wpdb->pmpro_discount_codes WHERE id = '" . esc_sql($code_id) . "' LIMIT 1");
$pmprosm_values = pmprosm_getValuesByMainLevel($level_id);
if(!is_array($pmprosm_values['sponsored_level_id']))
$sponsored_level_ids = array($pmprosm_values['sponsored_level_id']);
else
$sponsored_level_ids = $pmprosm_values['sponsored_level_id'];
//no sponsored levels to use codes for
if(empty($sponsored_level_ids) || empty($sponsored_level_ids[0]))
return;
//no uses for this code
if(empty($code->uses))
return;
$code_urls = array();
$pmpro_levels = pmpro_getAllLevels(true, true);
foreach($sponsored_level_ids as $sponsored_level_id)
{
$level_name = $pmpro_levels[$sponsored_level_id]->name;
$code_urls[] = array("name"=>$level_name, "url"=>pmpro_url("checkout", "?level=" . $sponsored_level_id . "&discount_code=" . $code->code));
}
//build message
$message = "<p>" . sprintf(__("Give this code to your sponsored members to use at checkout: %s", "pmpro_sponsored_members"), $code->code) . "<br />";
if(count($code_urls) > 1)
$message .= __("Or provide one of these direct links to register:", "pmpro_sponsored_members") . "</p>";
else
$message .= __("Or provide this direct link to register:", "pmpro_sponsored_members") . "</p>";
$message .= "<ul>";
foreach($code_urls as $code_url) {
$message .= "<li>" . $code_url['name'] . ": <strong>" . $code_url['url'] . "</strong></li>";
}
$message .= "</ul>";
return $message;
}
else
return "";
}
function my_pmpro_email_data($data, $email)
{
$data['sponsored_message'] = my_sponsored_email_shortcode($email);
return $data;
}
add_filter("pmpro_email_data", "my_pmpro_email_data", 10, 2);
function my_init()
{
remove_filter("pmpro_email_body", "pmprosm_pmpro_email_body", 10, 2);
}
add_action('init', 'my_init');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment