user3814097 user3814097 - 3 months ago 22
PHP Question

WooCommerce Subscriptions - Check if product already has an active subscriber

I am using a plugin "WooCommerce Subscriptions" and I want to check if the product already has the active subscriber in the system

I only want 1 subscriber per product. There is a filter available to check it but I don't know how to use it:
https://docs.woocommerce.com/document/subscriptions/develop/filter-reference/

How can I use that functions or hooks, to achieve this?

Thanks

Answer

— Updated 2 — (Mistakes correction and optimizations)

This custom made conditional function will return true if a subscription product is already actively used by a subscriber.

function has_an_active_subscriber( $product_id = null ){

    // Empty array to store ALL existing Subscription PRODUCTS
    $products_arr = array();


    $products_subscr = get_posts( array(
        'numberposts' => -1,
        'post_status' => 'publish',
        'post_type'   => array( 'product', 'product_variation' ),
        'meta_key' => '_subscription_price',
    ) );
    foreach( $products_subscr as $prod_subs ) {
        $products_arr[] = $prod_subs->ID;
    }

    // Testing if current product is a subscription product
    if (in_array( $product_id, $products_arr) ){

        // Declaring empties arrays
        $subscribers_arr = array(); // ALL subscribers IDS
        $active_subscriptions_arr = array(); // ALL actives subscriptions
        $active_subscription_products_arr = array(); // ALL actif subscription products IDS IDS
        $subscriber_subscriptions = array();

        // Getting arrays of "active" IDS for subscribers, subscriptions orders and subscription products
        $subscribers = get_users( array( 'role' => 'subscriber') );
        foreach( $subscribers as $subscriber ) {
            $subscriber_arr[] = $subscriber->ID;
            $subscriptions = wcs_get_users_subscriptions($subscriber->ID);
            foreach ($subscriptions as  $key => $subscription ){
                $subscription_status = $subscription->post->post_status;
                if ( $subscription_status == 'wc-active' ) { // active subscriptions only
                    $subscription_id = $subscription->post->ID;
                    $order_id = $subscription->order->post->ID; // order ID (corresponding to the subscription ID)
                    $active_subscriptions_arr[] = $subscription->post->ID;
                    $order_items = $subscription->order->get_items();
                    // Getting all the products in the Order
                    foreach ( $order_items as $item ) {
                        // $item_id = $item[product_id];

                        // Avoiding to add existing products in the array 
                        if( !in_array( $product_id, $active_subscription_products_arr ))
                            $active_subscription_products_arr[] = $item[product_id];
                    }
                }
            }
        }
    }
    if (in_array( $product_id, $active_subscription_products_arr ) ) return true;
    else return false;
}

This code goes on function.php of your active child theme (or theme) or also on any plugin file.

I just have used here wcs_get_users_subscriptions() native subscription function, to get the subscriptions for a defined user ID, in my code.


USAGE (for a defined $product_id variable)

If ( has_an_active_subscriber( $product->id ) ) { // or $product_id
    // This product is already used by an active subscriber
    // DO SOMETHING HERE
} else {
    // This product is NOT used
    // DO SOMETHING HERE
}

You can also replace $product_id by a product ID here for example if the ID of the product is 124):

If ( has_an_active_subscriber( 124 ) )  //do something

You can use this conditional function particularly, on add-to-cart (subscription) templates (that you will have to copy from the subscription plugin templates folder to your active theme's woocommerce template folder…)

All code is tested and fully functional
( (1) sorry when writing the code to a function, I have made some syntax mistakes. everything is corrected / (2) Added some codeoptimizations )

References:

Comments