Created
January 12, 2012 18:22
-
-
Save strangerstudios/1602202 to your computer and use it in GitHub Desktop.
Paid Memberships Pro and WP e-Commerce Integration Example
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
/* | |
Paid Memberships Pro/WP e-Commerce Integration | |
Description: | |
This group of functions will add a secure sign up link to the confirmation message for a specific product setup with WP E-commerce. | |
The session id from the order is passed to the PMPro checkout form, and will allow registration for a membership level that is otherwise disabled. | |
Once the user registers, the session id is added to the usermeta table as pmpro_sessionid_used so the session id can only be used once. | |
Requirements: | |
The Paid Memberships Pro plugin (v1.3.8 or higher) must be enabled. | |
The WP e-Commerce plugin (v3.8.7.5 or higher) must be enabled. | |
A product must be created. Set the $my_membership_product_id value below. | |
A membership level must be created and set to hidden. Change the id used in the functions below (6). | |
*/ | |
//this function checks the associated order for $_REQUEST['sessionid'] and sees if product $my_membership_product_id is on that order | |
function my_checkForMembershipProduct() | |
{ | |
global $wpdb; | |
if($_REQUEST['sessionid']) | |
{ | |
//my membership product | |
$my_membership_product_id = 204; //be sure to change this to your product id | |
//what was purchased? | |
$sqlQuery = "SELECT cc.prodid FROM " . WPSC_TABLE_CART_CONTENTS. " cc LEFT JOIN " . WPSC_TABLE_PURCHASE_LOGS. " pl ON cc.purchaseid = pl.id WHERE pl.sessionid = '" . $wpdb->escape($_REQUEST['sessionid']) . "'"; | |
$product_ids = $wpdb->get_col($sqlQuery); | |
if(!empty($product_ids)) | |
{ | |
if(in_array($my_membership_product_id, $product_ids)) | |
return true; | |
} | |
} | |
return false; | |
} | |
//this function updates the WP e-Commerce confirmation message to include the membership signup link | |
function my_wpsc_transaction_result_message($message) | |
{ | |
if(my_checkForMembershipProduct()) | |
{ | |
$message_html .= "\nFollow the link below to create your membership account:\n<" . pmpro_url("checkout", "?level=6&sessionid=" . esc_attr($_REQUEST['sessionid'])) . ">"; | |
} | |
return $message_html; | |
} | |
add_filter("wpsc_transaction_result_message_html", "my_wpsc_transaction_result_message_html"); | |
//this function updates the WP e-Commerce confirmation HTML message to include the membership signup link | |
function my_wpsc_transaction_result_message_html($message_html) | |
{ | |
if(my_checkForMembershipProduct()) | |
{ | |
$message_html .= "<br /><a href='" . pmpro_url("checkout", "?level=6&sessionid=" . esc_attr($_REQUEST['sessionid'])) . "'>Click here to create your membership account.</a>"; | |
} | |
return $message_html; | |
} | |
add_filter("wpsc_transaction_result_message_html", "my_wpsc_transaction_result_message_html"); | |
//this function checks for a sessionid parameter and if found adds a hidden field to the checkout form for it | |
function my_pmpro_checkout_boxes() | |
{ | |
if($_REQUEST['sessionid']) | |
{ | |
?> | |
<input type="hidden" name="sessionid" value="<?=esc_attr($_REQUEST['sessionid'])?>" /> | |
<?php | |
} | |
} | |
add_action("pmpro_checkout_boxes", "my_pmpro_checkout_boxes"); | |
//this function checks for a session id. if found, it checks if it is valid and hasn't been used before to sign up | |
function my_pmpro_checkout_level($pmpro_level) | |
{ | |
global $wpdb; | |
//session id? | |
if($_REQUEST['sessionid']) | |
{ | |
//is it valid? | |
if(empty($pmpro_level) && my_checkForMembershipProduct()) | |
{ | |
//has it been used yet? | |
$used = $wpdb->get_var("SELECT umeta_id FROM $wpdb->usermeta WHERE meta_key = 'pmpro_sessionid_used' AND meta_value ='" . $wpdb->escape($_REQUEST['sessionid']) . "' LIMIT 1"); | |
if(empty($used)) | |
{ | |
//okay, get the membership level | |
$pmpro_level = $wpdb->get_row("SELECT * FROM $wpdb->pmpro_membership_levels WHERE id = '" . $wpdb->escape($_REQUEST['level']) . "' "); | |
} | |
} | |
} | |
return $pmpro_level; | |
} | |
add_filter("pmpro_checkout_level", "my_pmpro_checkout_level"); | |
//this function adds an entry in the usermeta table that is checked so the session id cannot be used again to sign up | |
function my_pmpro_after_checkout() | |
{ | |
global $current_user; | |
//add usermeta so we know not to allow this session id anymore | |
if($_REQUEST['sessionid'] && $current_user->ID) | |
{ | |
add_user_meta($current_user->ID, "pmpro_sessionid_used", $_REQUEST['sessionid']); | |
} | |
} | |
add_action("pmpro_after_checkout", "my_pmpro_after_checkout"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you confirm if this still works?