Forked from strangerstudios/pmpro_after_checkout_send_gift_certificate_email.php
Last active
November 20, 2020 16:26
-
-
Save greathmaster/5ed9fc851027fe399f618103e5a96df9 to your computer and use it in GitHub Desktop.
After PMPro checkout, if the purchase is a gift level send an email to the gift recipient. (See additional functionality in comments.)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Send Gift Certificate email at checkout. | |
Requirements: | |
1. Paid Memberships Pro | |
2. The PMPro Gift Levels Addon | |
3. Register Helper Add On with a "gift_certificate_email" field added via custom code. | |
***MODIFIED*** | |
Adds the email shortcode !!code_url!! so we can potentially use it in a custom email template with PMPro Email Templates plugin. | |
*/ | |
function pmpro_after_checkout_send_gift_certificate_email($user_id) { | |
global $pmprogl_gift_levels, $pmpro_level, $wpdb; | |
//which level purchased | |
if(!empty($pmpro_level)) | |
$level_id = $pmpro_level->id; | |
elseif(!empty($_REQUEST['level'])) | |
$level_id = intval($_REQUEST['level']); | |
else | |
return; | |
//gift for this? if not, stop now | |
if(empty($pmprogl_gift_levels) || empty($pmprogl_gift_levels[$level_id])) | |
return; | |
//get the user's last purchased gift code | |
$gift_codes = get_user_meta($user_id, "pmprogl_gift_codes_purchased", true); | |
if(is_array($gift_codes)) | |
$last_code_id = end($gift_codes); | |
if(!empty($last_code_id)) | |
{ | |
$code = $wpdb->get_row("SELECT * FROM $wpdb->pmpro_discount_codes WHERE id = '" . intval($last_code_id) . "' LIMIT 1"); | |
if(!empty($code)) | |
{ | |
$code_url = pmpro_url("checkout", "?level=" . $pmprogl_gift_levels[$level_id]['level_id'] . "&discount_code=" . $code->code); | |
$user = get_userdata($user_id); | |
$to = get_user_meta($user->ID, 'gift_certificate_email', true); | |
$email = new PMProEmail(); | |
$email->email = $to; | |
$email->from = $user->user_email; | |
$email->fromname = $user->display_name; | |
$email->template = "gift_certificate"; | |
$email->subject = "Your membership to " . get_bloginfo('name') . "."; | |
$email->body = "<p>Use this link to setup your membership: <a href=\"" . $code_url . "\">" . $code_url . "</a></p>"; | |
//lets add the email shortcode !!code_url!! so we can use it in our custom template | |
$email->data['code_url'] = $code_url; | |
$worked = $email->sendEmail(); | |
} | |
} | |
} | |
add_action('pmpro_after_checkout', 'pmpro_after_checkout_send_gift_certificate_email', 15); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment