Category

How to check active subscriptions using YITH WooCommerce?

Recently while working over a membership site, i came up with a problem: “How to check active subscriptions using YITH WooCommerce plugin”? I search through the entire internet but couldn’t locate a good resource to solve this issue. Then i went and tried to look for any table in the database, but yet nothing exist in it either. So in short, YITH doesn’t use any additional table, it just uses the inbuilt tables i.e posts, meta posts etc.

Here is the snippet of code which can help you achieve this:

function CheckSubscriptionByEmail($email) {

// getting all the records of the user by email for the subscription
$subscriptions = get_posts( array(
‘numberposts’ => -1,
‘post_type’ => ‘ywsbs_subscription’, // Subscription post type
‘orderby’ => ‘post_date’, // ordered by date
‘order’ => ‘ASC’,
‘meta_query’ => array(
array(
‘key’ => ‘_billing_email’, //additional fields being used to get the data
‘value’ => $email,
‘compare’ => ‘=’
),
array(
‘key’ => ‘_status’,
‘value’ => ‘active’, //active subscriptions
‘compare’ => ‘=’
))
) );

// check if user has any active subscription
foreach ( $subscriptions as $post ) : setup_postdata( $post );
$nextdue = get_post_meta(get_the_id(), ‘_payment_due_date’,true);
$current_date = date(“Y-m-d”);
$date_to_compare = date(“Y-m-d”,strtotime($nextdue));
if (strtotime($date_to_compare) > strtotime($current_date)) {
echo “active”; //returns active
break;
}
endforeach;
wp_reset_postdata();

}

Some of the references that may help you in understanding this code:
How WP_Meta_Query functions?

Other related WooCommerce Problems:
How to Make your WooCommerce Shopping Cart Responsive?
Growth Hacking Techniques for your eCommerce Project