2

I've been banging my head for about a week now. any help would be greatly appreciated.

I have a WooCommerce store where I need to be able to update the user's membership status (canceled, active, etc) through Php.

I know I can grab their current status with:

wc_memberships_get_user_membership( $user_id, $membership_id );

and create new memberships with:

wc_memberships_create_user_membership( $args );

but I haven't found a way to just change the status.

thanks!

mujuonly
  • 11,370
  • 5
  • 45
  • 75
GeneralCan
  • 305
  • 6
  • 18

4 Answers4

5

For anyone else coming here. Here is my preferred solution. A WooCommerce Membership is just another WordPress post in your database and the membership status is stored as the post status. WooCommerce Memberships comes with the following statuses by default:

  • Active
  • Cancelled
  • Complimentary
  • Delayed
  • Expired
  • Paused
  • Pending Cancellation

In the database the statuses above look like this:

  • wcm-active
  • wcm-cancelled
  • wcm-complimentary
  • wcm-delayed
  • wcm-expired
  • wcm-paused
  • wcm-pending

All you need to do now is for example this:

$status = 'wcm-paused';
$update_args = array( 'ID' => $membership_id, 'post_status' => $status );
wp_update_post($update_args);

I hope this helps others.

P.s.: I always prefer using core functions over accessing the database directly. Unlike possible database changes, wp_update_post is a function that is fundamentally used in WordPress and will not disappear in the foreseeable future.

Jan Bkk
  • 83
  • 2
  • 8
1

Alright, so after searching around for more than a month, and going back and forth with Woocommerce's support team (they were no help :/ ), I came up with this solution:

Swimming around in the database I noticed that all memberships are just posts, and they have an ID and a post author. so I figured I could write up a little SQL to make it work.

using WordPres's built-in $wpdb class I was able to just update the field directly in the database:

first you have to include $wpdb in your function:

            global $wpdb;

then you put the query together, below is what it looks like in regular SQL

        UPDATE ie_posts
        SET post_status ='wcm-active'
        WHERE post_parent = 49 AND post_author = 49870

And here's what is looks like using the class:

        $wpdb->update('ie_posts',
            array('post_status' => $_status),
            array('post_parent' => $_membership_id,
                  'post_author' => $_user_id)
        );

I would advise trying this using a development copy of your database so you don't break anything.

deff not the prettiest way of doing it, but it works like a charm. if anyone has a better way of taking care of it, please let me know.

GeneralCan
  • 305
  • 6
  • 18
0

I'll try this way, for example to get any membership with a 'pending' status:

$user_id = get_current_user_id();

$args = array( 
    'status' => 'pending'
); 

$pending_memberships = wc_memberships_get_user_memberships( $user_id, $args );

Then and with a conditional check, you should be able to change this pending status to another one.

jjj
  • 965
  • 8
  • 23
0

Tested the following code with v1.17.1 (derived from class-wc-memberships-user-memberships.php)

$user_membership = wc_memberships_get_user_membership( $user_id, $membership_plan_id );
$user_membership->update_status( 'expired' );
David Buck
  • 3,752
  • 35
  • 31
  • 35