Skip to content

Instantly share code, notes, and snippets.

@wplit
Last active September 24, 2024 01:25
Show Gist options
  • Save wplit/f0c48531013e421f0d69341323582a78 to your computer and use it in GitHub Desktop.
Save wplit/f0c48531013e421f0d69341323582a78 to your computer and use it in GitHub Desktop.
allow people to purchase one individual tutorial by purchasing one membership
<?php
/* General idea
1. Create new membership called 'Single tutorial'.
2. In settings, make sure this membership can be purchased multiple times by same user.
2. Add a custom field to that registration form in settings, a text field named 'tutorial' for the post ID being purchased.
3. User will arrive at the form, with this tutorial custom field pre-filled based on the link they clicked from /?tutorial=POSTID
4. User meta data will store an array of post ids that user has purchased 'users_tutorials'.
5. Use the 'mepr-pre-run-rule-content' filter, to unblock any post for those users that have purchased
by checking the subscription and if the post IDs is in the user meta.
/* Code needed (working code, but needs to be tested fully)
*
*/
/*
use MP filter to change HTML of our new 'tutorial' text custom field so it pre-fills post ID from URL param
*/
add_filter('mepr_custom_field_html', 'lit_edit_tutorial_registration_field');
function lit_edit_tutorial_registration_field($html, $line, $value) {
$custom_field_name = 'tutorial';
if( $line->field_key == 'mepr_' . $custom_field_name) {
/* get post ID from URL /?tutorial=xxx */
$dynamic_post_id = intval( $_GET[ $custom_field_name ] );
$post_title = get_the_title( $dynamic_post_id );
/* prefill post ID as value for hidden text field, but show post title to user in readonly input they can see what they're buying */
$html = '<input type="text" value="' . esc_attr( $post_title ) . '" readonly />';
$html .= '<input type="hidden" name="mepr_tutorial" value="' . $dynamic_post_id . '">';
}
return $html;
}, 10, 3);
/* maybe unlock tutorial for user when viewing single post */
add_filter('mepr-pre-run-rule-content', 'lit_single_post_unlock', 11, 3);
function lit_single_post_unlock($protect, $post, $uri) {
/* only for members of 'Single tutorial' membership, change ID of 8888 */
if ( is_user_logged_in() && is_singular('post') && current_user_can("mepr-active","membership: 8888") ) {
// Retrieve the array of post IDs in user meta
$users_tutorials = get_user_meta( get_current_user_id(), 'users_tutorials', true);
/* check the user meta data 'users_tutorials' to find current post ID in the array */
if ( $users_tutorials && is_array( $users_tutorials ) ) {
/* if current post ID is in users tutorials, override all WP rules to unlock post for user */
if ( in_array( get_the_ID(), $users_tutorials) ) {
$protect = false;
}
}
}
return $protect;
}
/* on transaction complete,
* if user already logged in, we can add new post ID to the post meta 'users_tutorials'
* if user not logged in (new user) we will add new post ID on init.
* */
add_action( 'mepr-txn-status-complete', 'lit_mp_completed_transaction', 10, 1 );
function lit_mp_completed_transaction($txn) {
if ( is_user_logged_in() ) {
$userID = get_current_user_id();
if ( isset( $_POST['mepr_tutorial'] )) {
// get the value of our custom field
$post_id = intval( $_POST['mepr_tutorial'] );
// Retrieve the existing array of post IDs in user meta
$user_post_ids = get_user_meta($userID, 'users_tutorials', true);
if (!is_array($user_post_ids)) {
$user_post_ids = array(); // Initialize as an array if not set
}
// Add the new post ID to the array if it doesn't already exist
if (!in_array($post_id, $user_post_ids)) {
$user_post_ids[] = $post_id;
}
// Update the user meta with the new array of post IDs
update_user_meta($userID, 'users_tutorials', $user_post_ids);
}
}
}
/* add current value of 'mepr_tutorial' meta field (MP will have saved it to meta data after last purchase) to our users_tutorials array */
add_action('init', 'lit_save_tutorials_metadata');
function lit_save_tutorials_metadata() {
/* ideally this would only run on thank you page, so only after the transaction successful */
/* only for members of 'Single tutorial', change 8888 to match ID */
if ( is_user_logged_in() && current_user_can("mepr-active","membership: 8888") ) {
$post_id = get_user_meta(get_current_user_id(), 'mepr_tutorial', true);
$userID = get_current_user_id();
// Retrieve the existing array of post IDs in user meta
$user_post_ids = get_user_meta($userID, 'users_tutorials', true);
if (!is_array($user_post_ids)) {
$user_post_ids = array(); // Initialize as an array if not set
}
// Add the new post ID to the array if it doesn't already exist
if (!in_array($post_id, $user_post_ids)) {
$user_post_ids[] = $post_id;
}
// Update the user meta with new post ID
update_user_meta($userID, 'users_tutorials', $user_post_ids);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment