Skip Navigation

[Resolved] Set associated member profile to draft when WooCommerce Subscription ends

This thread is resolved. Here is a description of the problem and solution.

Problem:

Custom codes for WooCommerce Subscription plugin.

Solution:

It is a custom codes problem of WooCommerce Subscription plugin, see example here:

https://toolset.com/forums/topic/set-associated-member-profile-to-draft-when-woocommerce-subscription-ends-2/#post-2350401

Relevant Documentation:

This support ticket is created 2 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 11 replies, has 2 voices.

Last updated by JEN 2 years, 8 months ago.

Assisted by: Luo Yang.

Author
Posts
#2345457

JEN

Tell us what you are trying to do?
I have a listing site with WooCommerce and a custom post type. The idea is that when their subscription ends the listing gets set to Draft. Code from the below link was used to achieve this however what is actually happening is that listings are getting set to draft during the renewal process and not set back to Published. This is supposed to happen on role change but maybe it's being affected by the renewal process which sets the subscription from Active to On Hold, creates the renewal order, after success changes On Hold back to Active. I

What needs to happen is this:
- listings are set to Published if user role is Subscriber
- listings are set to Draft if user role is not Subscriber (or if user role is Customer)

Is there any documentation that you are following?
https://toolset.com/forums/topic/set-associated-member-profile-to-draft-when-woocommerce-subscription-ends/

Here is the code I'm using:

// set Listing to draft if subscription expires

add_action( 'woocommerce_subscription_status_updated', 'status_update_callback', 1, 3); 

function status_update_callback( $subscription, $old_status, $new_status ) {
	
    $updated_sub_user_id = $subscription->get_user_id();
 
    $args = array(
    'author'        =>  $updated_sub_user_id,
    'post_type'     => 'listing',
    'orderby'       =>  'post_date',
    'order'         =>  'ASC',
    'posts_per_page' => 1
    );
     
    $member_profile_post = get_posts( $args );
	
    if(!empty($member_profile_post)) {
        $mp_post_id = $member_profile_post[0]->ID; 
        $mp_post_args = array( 'ID' => $mp_post_id, 'post_status' => 'draft' );
        wp_update_post($mp_post_args);
    }
}

I have a staging site I can give access to if that's useful.

Thank you!

Jen

#2345785

Hello,

There isn't such kind of built-in feature within Toolset plugins.

I have checked your custom PHP codes, there is a PHP variable "$updated_sub_user_id", which should be a WordPress user ID.

You can set custom PHP codes to check if the "$updated_sub_user_id" has specific user role, for example:
https://wordpress.stackexchange.com/a/349582

Then change the post status as what you want.

For your reference.

#2346285

JEN

Hi Luo,

That's a disappointing response. If that's the case why didn't someone say so when I first asked this question? I would have used a different solution to build this site. It's too late for that now. I'm months into this project.

Is there any way Waqar can help with this? They helped the first time so would probably best able to assist now.

Thank you, Jen

#2346619

According to our support policy, we don't provide custom codes support:
https://toolset.com/toolset-support-policy/
It is easy to setup the custom codes, for example, you can change the custom codes as below:

// set Listing to draft if subscription expires
 
add_action( 'woocommerce_subscription_status_updated', 'status_update_callback', 1, 3); 
 
function status_update_callback( $subscription, $old_status, $new_status ) {
     
    $updated_sub_user_id = $subscription->get_user_id();
	$status = 'draft';
	if(has_user('subscriber', $updated_sub_user_id)){
		$status = 'publish';
	}
  
    $args = array(
    'author'        =>  $updated_sub_user_id,
    'post_type'     => 'listing',
    'orderby'       =>  'post_date',
    'order'         =>  'ASC',
    'posts_per_page' => 1
    );
      
    $member_profile_post = get_posts( $args );
     
    if(!empty($member_profile_post)) {
        $mp_post_id = $member_profile_post[0]->ID; 
        $mp_post_args = array( 'ID' => $mp_post_id, 'post_status' => $status );
        wp_update_post($mp_post_args);
    }
}

function has_user ($role=NULL, $user_id=NULL)
{
	if(empty($user_id)){
		$user = wp_get_current_user();
	} else {
		if(is_numeric($user_id) && $user_id == (int)$user_id) {
			$user = get_user_by('id', (int)$user_id);
		} else if(is_string($user_id) && $email = sanitize_email($user_id)) {
			$user = get_user_by('email', $email);
		} else {
			return false;
		}
	}
	
	if(!$user) return false;
	
	return in_array( $role, (array)$user->roles, true ) !== false;
}
#2349085

JEN

Hi Luo,

Thanks. If I understood Toolset and the support policy more when I began this project , I would have went with a different solution. When I read everything over prior to starting it seemed very possible to build a paid listings site in Toolset without much coding but it's actually been very challenging and required a lot of extra code.

I've tried out the code above and it successfully set the Listing to Draft when the author role changes from Subscriber to Customer due to a change in the subscription status. However, the reverse does not happen: If the role changes to Subscriber due to a change in the subscription status the associated Listing is not set to Published. Is there a way for that to happen too?

The reason it's needed is 1. If someone's subscription expires but then they renew, their listing should come back and 2. It turns out Woo is changing bo the subscription status and the user role during each renewal process. So they start out at active/subscriber, then get set to on hold/customer and then back to active/subscriber after the payment processes.

When I read the code above it seems like it would already work since that first if statement would set the status to publish? I must be missing something.

Thank you for your help! I've learned a lot on this site and took a course on php but I'm still learning.

Jen

#2349221

Please elaborate the new questions with more details:

If the role changes to Subscriber due to a change in the subscription status the associated Listing is not set to Published. Is there a way for that to happen too?

How and where do you change the subscription status?
Since I don't have a copy of "WooCommerce Subscription" plugin in my localhost, please provide a test site with the same problem, also provide detail steps to reproduce the same problem, where I can edit your custom PHP codes, thanks

#2349435

I can login into your website, please provide more detail steps to reproduce the same problem:
1) Where and how can I change the subscription status?
Is it this URL?
hidden link
If it is, which item I can change the status?

2) Where I can edit your custom PHP codes?

I need to debug the codes in a live website, so make sure you have already backup your website, then update here.

#2350171

JEN

Hi Luo,

Here's a link to change the subscription status for one of the users:
hidden link

On the left of the top area there is a dropdown called Subscription status. From here you can change the status to On Hold and then back to Active which will also toggle the user role from Subscriber to Customer and then back again. This mimics the process that happens when the subscription renews (which can be viewed here under Subscription notes: hidden link).

The PHP code is in:
/wp-content/themes/generatepress_ied/lib/woocommerce.php

Thank you again!

Jen

#2350369

Thanks for the details, I am checking it in your website, will update here if find anything

#2350401

Your staging website is quite slow, it will take 2~3 minutes to change the the subscription status, and hard to debug in such environment.

I have checked the custom codes as below:

add_action( 'woocommerce_subscription_status_updated', 'status_update_callback', 1, 3); 
  
function status_update_callback( $subscription, $old_status, $new_status ) {
	
	$data = $subscription->data;
	$updated_sub_user_id = $data['customer_id'];
    $status = 'draft';
    if(has_user('subscriber', $updated_sub_user_id)){
        $status = 'publish';
    }
   
    $args = array(
    'author'        =>  $updated_sub_user_id,
    'post_type'     => 'listing',
    'orderby'       =>  'post_date',
    'order'         =>  'ASC',
    'posts_per_page' => 1,
	'post_status' => array('draft', 'publish'),
    );
       
    $member_profile_post = get_posts( $args );
      
    if(!empty($member_profile_post)) {
        $mp_post_id = $member_profile_post[0]->ID; 
        $mp_post_args = array( 'ID' => $mp_post_id, 'post_status' => $status );
        wp_update_post($mp_post_args);
    }
}

Please test again, check if it is fixed, thanks

#2350403

Two places changed:
1) Get correct customer user ID
2) Query existed posts in both publish + draft status

#2351361

JEN

Hi Luo!

I tried it out and it works perfectly now.

I agree Flywheel staging sites are super slow. It's one of the few things I don't love about them.

Thank you so much!