Home › Toolset Professional Support › [Resolved] Delete users posts when Woocommerce subscription cancels
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 |
---|---|---|---|---|---|---|
- | 7:00 – 14:00 | 7:00 – 14:00 | 7:00 – 14:00 | 7:00 – 14:00 | 7:00 – 14:00 | - |
- | 15:00 – 16:00 | 15:00 – 16:00 | 15:00 – 16:00 | 15:00 – 16:00 | 15:00 – 16:00 | - |
Supporter timezone: Europe/London (GMT+00:00)
This topic contains 14 replies, has 2 voices.
Last updated by Nigel 6 years, 10 months ago.
Assisted by: Nigel.
Hi,
Ive got a form which allows users to post their content in to a custom post type "menu", However I am using Woocommerce Subscriptions, So Only those with a paid subscription can post a recipe into the post-type menus.
I now need to do a function where a users posts are deleted if their subscription is canceled.
Ive had a look at shortcodes etc for toolset and cant find anything.
I wrote this code so when the role goes from subscribed to member it would delete. But this code just deletes their post in the event of profile being updated.
Can someone help please. Woocommerce subscriptions said they dont support custom post types and user functions like this and I should talk to you.
function auto_expire_posts( $user_id, $new_role, $old_roles ) {
if ($new_role == 'member') {
}
global $wpdb;
$update = array(
'post_type' => 'menu',
'post_status' => 'trash'
);
$where = array(
'post_type' => 'menu',
'post_status' => 'publish',
'post_author' => $user_id
);
$updated = $wpdb->update( $wpdb->posts, $update, $where );
}
add_action( 'set_user_role', 'auto_expire_posts', 10, 3 );
Ive played around this afternoon This code kind of works now. However only in the backend of wordpress when I manually change the role from Subscriber to Premium.
The subscriptions plugin doesnt trigger this.
Though looking at the plugin I did a quick find phrase search and the plugin does not use the set_user_role hook by looks of it. Can I get some assistance please
function auto_expire_posts( $user_id, $new_role, $old_roles ) {
if ( ! in_array( subscribed', $old_roles ) ) {
return;
}
global $wpdb;
$update = array(
'post_type' => 'menu',
'post_status' => 'trash'
);
$where = array(
'post_type' => 'menu',
'post_status' => 'publish',
'post_author' => $user_id
);
$updated = $wpdb->update( $wpdb->posts, $update, $where );
}
add_action( 'set_user_role', 'auto_expire_posts', 10, 3 );
I have also tried to add a hook from Woocommerce subscriptions
add_action('woocommerce_subscription_status_cancelled', 'auto_expire_posts', 10, 3);
function auto_expire_posts( $user_id, $new_role, $old_roles ) {
if ( ! in_array( subscribed', $old_roles ) ) {
return;
}
global $wpdb;
$update = array(
'post_type' => 'menu',
'post_status' => 'trash'
);
$where = array(
'post_type' => 'menu',
'post_status' => 'publish',
'post_author' => $user_id
);
$updated = $wpdb->update( $wpdb->posts, $update, $where );
}
add_action( 'set_user_role', 'auto_expire_posts', 10, 3 );
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
Hi Chantelle
I'm not sure if your code is not working because the hook isn't being triggered or you code doesn't do what's expected.
To check the former I would add one or two error log messages in your code and see if they are being sent to the debug log or not.
I would be surprised if the WC Subscriptions changed user roles in such a non-standard way that the set_user_role hook was not triggered (but I haven't read through their code).
Assuming that it does, I would write the code something like the following (not tested), which would delete the user's posts when their role is changed from subscribed to member:
add_action( 'set_user_role', 'tssupp_autoexpire_posts', 10, 3); function tssupp_autoexpire_posts( $user_id, $role, $old_roles ){ $target_from = "subscribed"; $target_to = "member"; if ( $role == $target_to && in_array( $target_from, $old_roles ) ) { // get user's posts $user_posts = get_posts( array( 'post_author' => $user_id, 'post_type' => 'any' ) ); // delete posts foreach ($user_posts as $user_post) { wp_delete_post( $user_post->ID, true ); } } }
Do you want to try that?
Hi
Thanks.
Your code has not worked either.
I editted your code to this
add_action( 'set_user_role', 'tssupp_autoexpire_posts', 10, 3); function tssupp_autoexpire_posts( $user_id, $role, $old_roles ){ $target_from = "subscribed"; $target_to = "member"; if ( $role == $target_to && in_array( $target_from, $old_roles ) ) { // get user's posts $user_posts = get_posts( array( 'post_author' => $user_id, 'post_type' => 'menu' ) ); // delete posts foreach ($user_posts as $user_post) { wp_delete_post( $user_post->ID, true ); } } }
I then cancelled a subscription via the frontend of the user, Then went to backend comfirmed the cancellation via woocommerce - subscribtions. The user then went to member correctly.
Checked the posts and they still there, I then went and updated (didnt change anything just pressed update) the user via the backend and the posts were still there.
The code I had before was deleting the posts if the users profile was update via the backend and the posts went.
If this is going to be too much to fiddle with, is there a script which will run daily and remove members posts out of menus therefore only subscribed posts stay there?
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
It's just a question of finding the right hook to use that is triggered when the user's role is changed.
I looked through the Subscriptions plugin code and found the function wcs_update_users_role in the file wcs-user-functions.php.
After switching roles it triggers the following action:
do_action( 'woocommerce_subscriptions_updated_users_role', $role_new, $user, $role_old );
So I suggest you try using that hook, which has the old and new roles available for you to check.
Thank you
So the code I should test is this
add_action( 'set_user_role', 'tssupp_autoexpire_posts', 10, 3); do_action( 'woocommerce_subscriptions_updated_users_role', $role_new, $user, $role_old ); function tssupp_autoexpire_posts( $user_id, $role, $old_roles ){ $target_from = "subscribed"; $target_to = "member"; if ( $role == $target_to && in_array( $target_from, $old_roles ) ) { // get user's posts $user_posts = get_posts( array( 'post_author' => $user_id, 'post_type' => 'menu' ) ); // delete posts foreach ($user_posts as $user_post) { wp_delete_post( $user_post->ID, true ); } } }
or this?
add_action( 'woocommerce_subscriptions_updated_users_role', 'tssupp_autoexpire_posts', 10, 3); function tssupp_autoexpire_posts( $user_id, $role, $old_roles ){ $target_from = "subscribed"; $target_to = "member"; if ( $role == $target_to && in_array( $target_from, $old_roles ) ) { // get user's posts $user_posts = get_posts( array( 'post_author' => $user_id, 'post_type' => 'menu' ) ); // delete posts foreach ($user_posts as $user_post) { wp_delete_post( $user_post->ID, true ); } } }
I tried this, also changing $old_roles to $role_old to match how subscriptions have it.
Still not working (even when updating on backend, its not working)
add_action( 'woocommerce_subscriptions_updated_users_role', 'tssupp_autoexpire_posts', 10, 3); function tssupp_autoexpire_posts( $user_id, $role, $role_old ){ $target_from = "subscribed"; $target_to = "member"; if ( $role == $target_to && in_array( $target_from, $role_old ) ) { // get user's posts $user_posts = get_posts( array( 'post_author' => $user_id, 'post_type' => 'menu' ) ); // delete posts foreach ($user_posts as $user_post) { wp_delete_post( $user_post->ID, true ); } } }
The closest Ive got is
However this only works when the subscription is cancelled you then go to Dashboard, Users and update the user
or if you change the role via dashboard users.
Either way I have to trigger the profile update in the backend (dashboard) which isnt what I want, the cancellation of the subscription needs to trigger it.
add_action('woocommerce_subscription_status_cancelled', 'auto_expire_posts', 10, 3); function auto_expire_posts( $user_id, $new_role, $old_roles ) { if ( ! in_array( subscribed', $old_roles ) ) { return; } global $wpdb; $update = array( 'post_type' => 'menu', 'post_status' => 'trash' ); $where = array( 'post_type' => 'menu', 'post_status' => 'publish', 'post_author' => $user_id ); $updated = $wpdb->update( $wpdb->posts, $update, $where ); } add_action( 'set_user_role', 'auto_expire_posts', 10, 3 );
Ive tried This.
function auto_expire_posts( $user_id, $new_role, $role_old ) { if ( ! in_array( 'subscribed', $role_old ) ) { return; } global $wpdb; $update = array( 'post_type' => 'menu', 'post_status' => 'trash' ); $where = array( 'post_type' => 'menu', 'post_status' => 'publish', 'post_author' => $user_id ); $updated = $wpdb->update( $wpdb->posts, $update, $where ); } add_action( 'woocommerce_subscriptions_updated_users_role', 'auto_expire_posts', 10, 3 ); add_action( 'set_user_role', 'auto_expire_posts', 10, 3 );
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
Hi Chantelle
The woocommerce_subscriptions_updated_users_role action has different arguments in a different order, so you need to rewrite the code accordingly.
(You can see the arguments by looking at where the action is used, the do_action line I shared earlier.)
I don't have a test site with WooCommerce Subscriptions set up to check, but looking at the plugin code I expect that this code should work:
add_action( 'woocommerce_subscriptions_updated_users_role', 'tssupp_autoexpire_posts', 10, 3); function tssupp_autoexpire_posts( $role_new, $user, $role_old ){ error_log("User role updated from " . $role_old . " to " . $role_new); $target_from = "subscribed"; $target_to = "member"; if ( $role_new == $target_to && $role_old == $target_from ) { error_log("Condition met"); // get user's posts $user_posts = get_posts( array( 'post_author' => $user->ID, 'post_type' => 'any' ) ); // delete posts foreach ($user_posts as $user_post) { wp_delete_post( $user_post->ID, true ); } } }
Note that I have included a couple of messages to send to the debug.log to help identify why the code doesn't work if you find it doesn't.
Can you try this and let me know what you find. If the code doesn't work, what is sent to debug.log?
If the code does work you can delete the error_log lines.
This code gives me an error 500?
I can make a dummy site with woocommerce subscriptions on to test and give you credentials if that would help?
Thanks
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
Sorry, minor typo on line 9 with a surplus ')'. The debug.log would have shown that.
Replace it with this:
if ( $role_new == $target_to && $role_old == $target_from ) {
Do you want to try again. If it doesn't work then let me know what messages are being sent to the debug.log, and if they are not helpful then, yes, you can set up a test site, but let's see first.
Here is the error log
I uploaded it as we transfer and only put the logs from today on - Sorry I dont really understand them
hidden link
Thanks
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
Hi Chantelle
Your log is filled with so many warnings and errors (unrelated to Toolset) that it is very difficult to see what is going on with the code we are trying to add, and where there are problems elsewhere in the code it means the code we are adding may not run as intended.
The only relevant part I saw was the following:
[23-Jan-2018 13:41:39 UTC] User role updated from supplier to premium_supplier [23-Jan-2018 13:41:39 UTC] User role updated from supplier to premium_supplier
Presumably some action you took triggered that.
I would expect to see the same when changing role from subscribed to member, but there is no evidence of that, but because of all the other problems reported in the logs it is hard to know what to expect.
So, I suggest you
- disable any and all custom code you have added to your site
- disable any plugin that is not essential to the basic functionality of your site using WC Subscriptions
- delete the current error log file
- perform basic actions on your site including adding users and subscriptions etc.
- check the error log again. If it looks anything like the version that you showed me you should contact support of WC Subscriptions and show it to them
- at the same time ask them about the 'woocommerce_subscriptions_updated_users_role' hook.
Confirm with them that the hook is triggered in the circumstances you expect/need. They don't need to know the details of what you want to use the hook for, you just need to confirm that it is triggered when required, i.e. when the subscription is cancelled. If they don't have a hook that is triggered then, you won't be able to do what you are aiming to.
Let's keep this thread open and when you have completed the above let me know and then we can proceed.