Hi Jason,
I have a similar query, but I want to check if the user has an active subscription when they try to purchase the follow on product, not just whether they might have purchased a subscription in the past and since cancelled it. Does this plugin have that capability to check that condition $has_sub = wcs_user_has_subscription( ”, ”, ‘active’ );? Thanks
Guess would use this sort of code:
/**
* Check if a user has a subscription, optionally to a specific product and/or with a certain status.
*
* @param int $user_id (optional) The ID of a user in the store. If left empty, the current user’s ID will be used.
* @param int $product_id (optional) The ID of a product in the store. If left empty, the function will see if the user has any subscription.
* @param mixed $status (optional) A valid subscription status string or array. If left empty, the function will see if the user has a subscription of any status.
* @since 2.0
*
* @return bool
*/
function wcs_user_has_subscription( $user_id = 0, $product_id = ”, $status = ‘any’ ) {
$subscriptions = wcs_get_users_subscriptions( $user_id );
$has_subscription = false;
if ( empty( $product_id ) ) { // Any subscription
if ( ! empty( $status ) && ‘any’ != $status ) { // We need to check for a specific status
foreach ( $subscriptions as $subscription ) {
if ( $subscription->has_status( $status ) ) {
$has_subscription = true;
break;
}
}
} elseif ( ! empty( $subscriptions ) ) {
$has_subscription = true;
}
} else {
foreach ( $subscriptions as $subscription ) {
if ( $subscription->has_product( $product_id ) && ( empty( $status ) || ‘any’ == $status || $subscription->has_status( $status ) ) ) {
$has_subscription = true;
break;
}
}
}
return apply_filters( ‘wcs_user_has_subscription’, $has_subscription, $user_id, $product_id, $status );
}
-
This reply was modified 4 years, 6 months ago by dubaidogfish.
-
This reply was modified 4 years, 6 months ago by dubaidogfish.