Last active
November 25, 2020 22:22
-
-
Save strangerstudios/1474f1de45f6fcf314a08079e8258020 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.
This file contains hidden or 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. | |
*/ | |
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>"; | |
$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