Support » Plugin: Pay For Post with WooCommerce » Verify if user has access to a page from outside that page

  • Resolved donnietheg

    (@donnietheg)


    I am selling a drumming course. There are 4 levels in the course (beginner, intermediate, advanced and master). Each level has a page with links to 9 episodes. These 4 pages with links to the 9 episodes are my pay-per-post pages. Users who have not purchased the level do not have access to the links to 9 episodes. All’s good up to there.

    The episode pages have a series of exercises that are generated using a php template (i.e. episode.php). These pages are easily accessible if someone can figure out the url structure (e.g. /beginner/episode-title/). So they could access all my content without paying. I would like to be able to verify if the user has purchased the level for the episode from within the episode.php template.

    I know that there is the helper <?php if(Woocommerce_Pay_Per_Post_Helper::has_access()): ?> which can verify if a user has access to the purchased page when we are on the pay-per-post page. Is there way to check if a user has access to a page when we are not on a pay-per-post page. For example, is there a variable which is available outside this page that I could use to validate that the user has access to the episode pages?

    • This topic was modified 3 months, 2 weeks ago by donnietheg.
    • This topic was modified 3 months, 2 weeks ago by donnietheg.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Matt Pramschufer

    (@mattpramschufer)

    Hey @donnietheg sorry for the delay! So what you should do is look into plugins/pay-for-post/includes/class-woocommerce-pay-per-post-helper.php

    You should see a bunch of helper functions you can use inside your code. What you want to do is simply pass in the post_id to the function. i.e.

    public static function has_access($post_id = null, $track_page_view = true) {

    Thread Starter donnietheg

    (@donnietheg)

    Thanks @mattpramschufer ! That worked.

    For others, I made a taxonomy for the pay-per-post post and used it for the pages that the pages the pay-per-post post gives access to. I needed to simply get the id of the corresponding pay-per-post post when in the one of the pages. Here is how it looks…

    <?php  // get the episode_level taxonomy term of the current Level page (e.g. intermediate)
        $terms = get_the_terms($post->ID, 'episode_level');
        if (!empty($terms)) {
          $term = array_shift($terms);
        }
        $episode_free   = get_field('episode_free'); // check if this episode has acf radio button field selected as free
        $level          = $term->slug;
        
        // Prepare to HIDE THIS EPISODE PAGE content if user has not purchased the parent Level
        // Check the value of episode_level taxonomy and define the corresponding the hardcoded id of the parent Level post
        if ($level == 'beginner') :
          $level_purchased_id = 190; // IMPORTANT: change these hardcoded ids to correspond to the Pay-Per-Post page which is the parent of the episode
          elseif ($level == 'intermediate'):
          $level_purchased_id = 187;
          elseif ($level == 'advanced'):
          $level_purchased_id = 189;
          elseif ($level == 'master'):
          $level_purchased_id = 188;
        endif;
        if(Woocommerce_Pay_Per_Post_Helper::has_access($level_purchased_id) or ($episode_free == 'isfree')): // if the user has paid for this post or it is free display Episode content
        // Query custom-post Episode for this Episode single page (e.g./level/intermediate/)
    while (have_posts()) : the_post(); ?>
    [my page code here]
    endwhile; 
          // This is what users who have not paid for the parent Level of this Episode see, or if the Episode is free
          else : echo('Sorry! You can not see this until you <a href="' . site_url() . '/wp-admin">&nbsp;login</a>&nbsp; or  <a href="' . site_url() . '">&nbsp;purchase it</a>.');
        endif; ?>
    
    Plugin Author Matt Pramschufer

    (@mattpramschufer)

    Awesome Job! Thanks for contributing to everyone!

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.