6

I am using WooCommerce Subscriptions plugin and I am trying to get the customer or userid of a given wc_subscription.

Here is the code I have been using but fails:

add_action( 'woocommerce_scheduled_subscription_trial_end', 'registration_trial_expired', 100 );
function registration_trial_expired( $wc_subscription ) {
    mail("example@gmail.com", "Expired", "Someone's order has expired");
    $userid = $wc_subscription->customer_user;
    mail("example@gmail.com", "Expired", "Someone's order has expired with customer".$userid);
...
}

I thought $wc_subscription->customer_user will have the userid but it is empty. In fact stops the code from continuing.

How can I get the user ID with $wc_subscription?

Cœur
  • 37,241
  • 25
  • 195
  • 267
user3072613
  • 363
  • 2
  • 6
  • 18

3 Answers3

7

As class WC_Subscription methods are inherited from WC_Abstract_Order and WC_Order classes, you can use get_user_id() method this way:

$userid = $wc_subscription->get_user_id();

This code is tested and works with WC_Subscription instance object

So your code will be:

add_action( 'woocommerce_scheduled_subscription_trial_end', 'registration_trial_expired', 100 );
function registration_trial_expired( $wc_subscription ) {
    mail("example@gmail.com", "Expired", "Someone's order has expired");
    $userid = $wc_subscription->get_user_id(); // <= HERE
    mail("example@gmail.com", "Expired", "Someone's order has expired with customer".$userid);
    // ...
}

Update (on OP's comment)

As the argument $wc_subscription was the subscription ID (and not the Subscription object).

So I have changed the code to:

add_action( 'woocommerce_scheduled_subscription_trial_end', 'registration_trial_expired', 100 );
function registration_trial_expired( $subscription_id ) {

    // Get an occurrence of the WC_Subscription object
    $subscription = wcs_get_subscription( $subscription_id );
    
    // Get the user ID (or customer ID)
    $user_id = $subscription->get_user_id();

    // The email adress
    $email = 'example@gmail.com';

    // The theme domain (replace with your theme domain for localisable strings)
    $domain = 'woocommerce';
    
    mail( $email, 'Expired', __("Someone’s order has expired", $domain);
    mail( $email, 'Expired', __("Someone’s order has expired with customer", $domain) . $user_id );

    // ...
}
Community
  • 1
  • 1
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hi. Thanks for the answer. Could please edit it and replace my email with: example@gmail.com I made a mistake not replacing it. $wc_subscription is actually an id. Not the actual WC Subscription object. So using this at the very top fixes $wc_subscription = wcs_get_subscription($wc_subscriptionid); – user3072613 Mar 30 '17 at 06:20
  • @user3072613 I have updated my answer replacing the email address and changing the code. Please take a look, It should be correct now, based on your comment. – LoicTheAztec Mar 30 '17 at 07:28
3

WC_Subscription is a expanded version of WC_ORDER, thus you can use the same calls as WC_ORDER.

Your code tweaked:

 add_action( 'woocommerce_scheduled_subscription_trial_end', 'registration_trial_expired', 100 );
 function registration_trial_expired( $wc_subscription ) 
    {
    $order_billing_email = $wc_subscription->get_billing_email(); 
    $User      = get_user_by( 'email', $order_billing_email );             /
    $FirstName = $User->first_name;
    $LastName  = $User->last_name;
    $UserId    = $User->ID;
    }
Debbie Kurth
  • 403
  • 3
  • 16
  • sometimes the subscription can be made with an email while the account is created with another email. In this case the get_user_by will return null ( I just fixed a similar bug). So the solution is to get user_id with $wc_subscription->get_user_id(); – Crerem Dec 29 '21 at 10:32
1

Retrieve the current user object (WP_User). Wrapper of get_currentuserinfo() using the global variable $current_user.

wp_get_current_user();

But it may be deprecated so You can derived from

$userdata = WP_User::get_data_by( $field, $value );
Pranav Bhatt
  • 715
  • 4
  • 8
  • You did not answer the question. wp_get_current_user(), return the current logged in user, what he is looking for is the user in subscription – Karue Benson Karue Feb 25 '21 at 05:58