Correct function to get the user's latest Woocommerce Subscription?

I've a members' only subscription site, where the member can only have one active subscription, and I need to

I've a members' only subscription site, where the member can only have one active subscription, and I need to be able to get the status of the subscription.

I've been through all the documentation and found the wcs_get_users_subscriptions() function, which returns an array of all subscription products for the given user, in reverse date order (ie, latest is always first).

I settled on this code which, whilst it works, I know is absolutely the wrong way to do it:

$subs = wcs_get_users_subscriptions( $user_id );
$subs = (object) array_shift( $subs );
$subs = (object) $subs->data;
$return = $subs->status;

Does anyone know the right way?

I've a members' only subscription site, where the member can only have one active subscription, and I need to be able to get the status of the subscription.

I've been through all the documentation and found the wcs_get_users_subscriptions() function, which returns an array of all subscription products for the given user, in reverse date order (ie, latest is always first).

I settled on this code which, whilst it works, I know is absolutely the wrong way to do it:

$subs = wcs_get_users_subscriptions( $user_id );
$subs = (object) array_shift( $subs );
$subs = (object) $subs->data;
$return = $subs->status;

Does anyone know the right way?

Share Improve this question edited Jun 13, 2018 at 16:21 Peter HvD asked Jun 5, 2018 at 16:17 Peter HvDPeter HvD 1,1211 gold badge6 silver badges16 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 12

use this code

$users_subscriptions = wcs_get_users_subscriptions($user_id);

retrieve the data using foreach

foreach ($users_subscriptions as $subscription){
  if ($subscription->has_status(array('active'))) {
         echo $subscription->get_id(); 
  }
}

There is some function to access subscription data. like get_id(),get_date('end') (to get last date of subscription ).

I hope its help you well.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744819836a4595547.html

相关推荐

  • Correct function to get the user's latest Woocommerce Subscription?

    I've a members' only subscription site, where the member can only have one active subscription, and I need to

    2天前
    70

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信