Search code examples
phpwordpresswoocommercesubscription

Woocommerce Subscription access from external php


I have a Wordpress website with Woocommerce and Woocommerce Subscription plugins.

I need to write some PHP script that will check if a user has active subscription status.

From the database, I would like to get information: user id, status (if it's active - if payment was made and the subscription was renewed), end date of subscription...

The problem is that I don't even know where subscription info is saved?

Can somebody write me a snippet of code, that includes a query that will give me a list of users that subscribed, with the necessary information mentioned earlier?

I will post an update with code later, for now, I just need help with query and guidance where to look in the database tables.


Solution

  • Here is my solution (not saying that it's the best, but for me, it solved the problem that I had).

    Code in function.php (WP child theme)

    add_action('init','getWPuser');
    function getWPuser(){
    $current_user = wp_get_current_user();
    return $current_user;
    }
    

    Custom website

    Code in my custom site that needs information about the user.

    require_once '../wp-load.php';
    require_once '../wp-content/themes/h-code-child/functions.php';
    require_once '../wp-includes/pluggable.php';
    require_once '../wp-blog-header.php';
    $current_user = getWPuser();
    

    Saving user ID information.

    $logged_user_id = $current_user->ID;
    

    Checking on specific subscription if it's active.

    $has_free = wcs_user_has_subscription( $logged_user_id, $product_id, 'active' );