Created
December 30, 2020 22:23
-
-
Save weo3dev/a9f23519e664ea2bc89b2752e0b86d0d to your computer and use it in GitHub Desktop.
WP Customization for PFC Legacy
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
We're using the wp snippets plugin, per PMP prior advice. | |
/* SNIPPET 1 */ | |
/* Custom Fields for Membership Registration */ | |
function my_pmprorh_init_gift_recipient_email() { | |
// don't break if Register Helper is not loaded | |
if ( ! function_exists( 'pmprorh_add_registration_field' ) ) { | |
return false; | |
} | |
// bail if Gift Memberships not configured | |
global $pmprogl_gift_levels; | |
if ( ! function_exists( 'pmprogl_pmpro_after_checkout' ) | |
|| ! function_exists( 'pmpro_after_checkout_send_gift_certificate_email' ) | |
|| empty( $pmprogl_gift_levels ) | |
|| ! is_array( $pmprogl_gift_levels ) | |
|| count( $pmprogl_gift_levels ) < 1 | |
) { | |
return false; | |
} | |
// clear value from previous gift level purchase | |
if ( is_user_logged_in() ) { | |
global $current_user; | |
if ( ! empty( get_user_meta( $current_user->ID, 'gift_certificate_email' ) ) ) { | |
delete_user_meta( $current_user->ID, 'gift_certificate_email' ); | |
} | |
} | |
// define the fields | |
$fields = array(); | |
// TEXT FIELD - Basic Example | |
$fields[] = new PMProRH_Field( | |
'gift_certificate_email', // input field name, used as meta key | |
'text', // field type | |
array( | |
'label' => 'Gift Recipient Email (Optional)', | |
'hint' => 'Sends a gift certificate to this email address after checkout.', // string for <label></label> | |
'levels' => array_keys( $pmprogl_gift_levels ), | |
// 'required' => true, | |
) | |
); | |
foreach ( $fields as $field ) { | |
pmprorh_add_registration_field( | |
'checkout_boxes', // location on checkout page | |
$field // PMProRH_Field object | |
); | |
} | |
unset( $field ); | |
// that's it. see the PMPro Register Helper readme for more information and examples. | |
} | |
add_action( 'init', 'my_pmprorh_init_gift_recipient_email' ); | |
/* SNIPPET 2 */ | |
/* Send Gift Certificate Email at Checkout */ | |
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