0
votes

I have a couple of products each linked with an WooCommerce membership plan, each plan restricts a specific post category, so the customer can view posts of a category only after purchased the product whose membership plan restricts that category.

Now for every new post I would like to send an email to the customer that has purchased the product,restricting that category of posts, a new post has been published. Is there a way to accomplish this?

To go into more details its like this:

I have Product_A and Product_B

2 Post categories: Literature and Math

2 Membership plans: Plan_Prod_A and Plan_Prod_B

Plan_Prod_A restricts the category Literature

Plan_Prod_B restricts Math

To view posts from the Literature category the customer must purchase Product_A that will grant him access to the membership plan Plan_Prod_A.

Now after purchased I need that for as long as the membership lasts,for any new published posts from the Literature category an email must be sent to the customer with the link of the post to notify him that a new post has been published.

1

1 Answers

0
votes

i managed to do it like this tho would it cause any issues on the long run or is it ok?

function post_unpublished( $new_status, $old_status, $post ) {
            if ( $old_status != 'publish'  &&  $new_status == 'publish' ) {
                    $user_id = get_current_user_id();
                    $post_id = $post->ID;   
               //     if(get_post_meta($post_id,'_wc_memberships_force_public',true)=='yes') return true;
                    $args = array( 'status' => array( 'active' ));   
                    $plans = wc_memberships_get_user_memberships( $user_id, $args );
                    $user_plans = array();
                    foreach($plans as $plan){
                        array_push($user_plans,$plan->plan_id);
                    }

                    $rules = wc_memberships()->get_rules_instance()->get_post_content_restriction_rules( $post_id );

                    foreach($rules as $rule){
                        if(in_array($rule->get_membership_plan_id(), $user_plans)){
                            $post_url = get_permalink( $post_id );                       
                            $subject = 'New Post';                   
                            $message = "A new post came out:\n\n";
                            $message .= $post->post_title . ": " . $post_url;
                            wp_mail( 'email', $subject, $message ); 
                        }

                    }       


            }
        }
        add_action( 'transition_post_status', 'post_unpublished', 10, 3 );